Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:17:41+00:00 2026-06-03T10:17:41+00:00

I need to create a BEFORE INSERT trigger in SQLAlchemy: DELIMITER | CREATE TRIGGER

  • 0

I need to create a BEFORE INSERT trigger in SQLAlchemy:

DELIMITER |
CREATE TRIGGER set_rank BEFORE INSERT ON authors
FOR EACH ROW BEGIN
    IF NEW.rank = 0 THEN
        SET NEW.rank = (SELECT IFNULL(MAX(a.rank),0) + 1
                        FROM authors AS a
                        WHERE a.id = NEW.pub_id);
    END IF;
END |
DELIMITER ;

Executing the code in mysql-workbench works just fine, but renders a “You have an error in your SQL syntax” exception when executed in my actual code:

from sqlalchemy.sql.expression import text

connection = db.session.connection()
text(..., connection).execute();

Running SELECT SLEEP(1), CREATE TABLE test (id INT) or even USE some_db works just fine.

This the the mysql error message I get:

(1064, “You have an error in your SQL syntax; … near ‘DELIMITER |\nCREATE TRIGGER set_rank BE…”)

I am quite sure that I use the DELIMITER incorrectly. What is the right syntax in this very context / How to fix my problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:17:42+00:00Added an answer on June 3, 2026 at 10:17 am

    You really honestly don’t need the DELIMITER. Thats only for the command line client. You as a programmer will be dividing up statements, so the delimiters are otherwise ignored.

    >>> from sqlalchemy import *
    >>> 
    >>> trigger_text = """
    ... CREATE TRIGGER set_rank BEFORE INSERT ON authors
    ... FOR EACH ROW BEGIN
    ...     IF NEW.rank = 0 THEN
    ...         SET NEW.rank = (SELECT IFNULL(MAX(a.rank),0) + 1
    ...                         FROM authors AS a
    ...                         WHERE a.id = NEW.pub_id);
    ...     END IF;
    ... END
    ... """
    >>> 
    >>> metadata = MetaData()
    >>> authors = Table("authors", metadata,
    ...     Column("id", Integer, primary_key=True),
    ...     Column("rank", Integer),
    ...     Column("pub_id", Integer))
    >>> 
    >>> engine = create_engine("mysql://root@127.0.0.1/test", echo=True)
    >>> authors.create(engine)
    2012-05-04 17:11:41,093 INFO sqlalchemy.engine.base.Engine 
    CREATE TABLE authors (
        id INTEGER NOT NULL AUTO_INCREMENT, 
        rank INTEGER, 
        pub_id INTEGER, 
        PRIMARY KEY (id)
    )
    
    
    2012-05-04 17:11:41,093 INFO sqlalchemy.engine.base.Engine ()
    2012-05-04 17:10:51,376 INFO sqlalchemy.engine.base.Engine COMMIT
    >>> engine.execute(trigger_text)
    2012-05-04 17:11:41,159 INFO sqlalchemy.engine.base.Engine 
    CREATE TRIGGER set_rank BEFORE INSERT ON authors
    FOR EACH ROW BEGIN
        IF NEW.rank = 0 THEN
            SET NEW.rank = (SELECT IFNULL(MAX(a.rank),0) + 1
                            FROM authors AS a
                            WHERE a.id = NEW.pub_id);
        END IF;
    END
    
    2012-05-04 17:11:41,159 INFO sqlalchemy.engine.base.Engine ()
    2012-05-04 17:11:41,312 INFO sqlalchemy.engine.base.Engine COMMIT
    <sqlalchemy.engine.base.ResultProxy object at 0x2be1ed0>
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Attempting to run this SQL: DELIMITER $$ CREATE TRIGGER crypt_f BEFORE INSERT ON c_data_test
I have the following code. I need to create list separators before A elements,
I'm doing a bulk insert but before inserting into the actual table I need
Someone told me that I need to use a delimiter in my trigger. I'm
mysql ver is 5.0.67 my trigger code is : DELIMITER // CREATE TRIGGER upd_price
I created a trigger for a table (Person) so that each time a new
cat myfile.sql DELIMITER $$ USE `AA4`$$ DROP TRIGGER /*!50032 IF EXISTS */ `AT_Card_INSERT_Trigger`$$ CREATE
e.g. I have a table: create table test (testdata varchar2(255)); then I need an
For an application I'm developing I need to create a signature file before I
I need create custom dialog and put JPanel into it. Is it possible?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.