2

I'm trying to implement a few simple SQL insert statements with python-mariadb-connector but cannot figure out what I'm doing wrong.

The database looks like this:

SET FOREIGN_KEY_CHECKS = false;

CREATE OR REPLACE TABLE `forums` (
  `id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE OR REPLACE TABLE `accounts` (
  `id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE OR REPLACE TABLE `posts` (
  `id` int(10) unsigned NOT NULL,
  `forum_id` int(10) unsigned NOT NULL,
  `account_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `posts_forum_fk` (`forum_id`),
  KEY `posts_account_fk` (`account_id`),
  CONSTRAINT `posts_account_fk` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`id`),
  CONSTRAINT `posts_forum_fk` FOREIGN KEY (`forum_id`) REFERENCES `forums` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE OR REPLACE TABLE `comments` (
  `id` int(10) unsigned NOT NULL,
  `post_id` int(10) unsigned NOT NULL,
  `account_id` int(10) unsigned NOT NULL,
  `parent_id` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `comments_post_fk` (`post_id`),
  KEY `comments_account_fk` (`account_id`),
--  KEY `comments_comments_fk` (`parent_id`),
--  CONSTRAINT `comments_comments_fk` FOREIGN KEY (`parent_id`) REFERENCES `comments` (`id`),
  CONSTRAINT `comments_account_fk` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`id`),
  CONSTRAINT `comments_post_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

SET FOREIGN_KEY_CHECKS = true;

The code for inserting data looks like this:

import mariadb


config = {
    "user": "db_user_name",
    "password": "db_passwd",
    "host": "db_host",
    "port": 3306,
    "database": "db_name"
}


if __name__ == '__main__':
    with mariadb.connect(**config) as conn:
        cur = conn.cursor()
        cur.executemany(
            "INSERT INTO `forums` (`id`) VALUES (?)",
            [(1,), (2,), (3,)]
        )
        cur.executemany(
            "INSERT INTO `accounts` (`id`) VALUES (?)",
            [(1,), (2,), (3,), (4,)]
        )
        cur.executemany(
            "INSERT INTO `posts` (`id`, `forum_id`, `account_id`) VALUES (?, ?, ?)",
            [(6, 3, 1)]
        )
        cur.executemany(
            "INSERT INTO `comments` (`id`, `post_id`, `account_id`, `parent_id`) VALUES (?, ?, ?, ?)",
            [(1, 6, 1, None), (2, 6, 2, 1)]
        ) # exception happens here

When executing this, I get the following error:

Traceback (most recent call last):
  File ".../db_test.py", line 28, in <module>
    cur.executemany(
mariadb.DatabaseError.DataError: Invalid parameter type at row 2, column 4

Im not sure how executemany is implemented but I think it should do something similar to the following SQL-query:

INSERT INTO `forums` (`id`) VALUES (1), (2), (3);

INSERT INTO `accounts` (`id`) VALUES (1), (2), (3), (4);

INSERT INTO `posts` (`id`, `forum_id`, `account_id`) VALUES (6, 3, 1);

INSERT INTO `comments` (`id`, `post_id`, `account_id`, `parent_id`)
VALUES (1, 6, 1, NULL), (2, 6, 2, 1);

which works just fine for me...

Is it a bug or am I doing something wrong here?

1
  • Elsewhere I see '%s' being used instead of '?' for the parameters. Why is this? Apr 1, 2023 at 16:31

1 Answer 1

1

It took me a while

    cur.executemany(
        "INSERT INTO `comments` (`id`, `post_id`, `account_id`, `parent_id`) VALUES (?, ?, ?, ?)",
        [(1, 6, 1, None), (2, 6, 2, 1)]
    ) 

But in your comment table, you have this constraint


CONSTRAINT `comments_comments_fk` FOREIGN KEY (`parent_id`)
REFERENCES `comments` (`id`),

Before you can enter (2, 6, 2, 1) the tuple (1, 6, 1, None) has to already commit in the database and in a bulk insert the commit is made after all inserts are in the database, but the first tuple isn't at the time not there

so if you make this, both rows will appear in the database(i also committed all other tables after bulk insert):

MariaDB

mycursor.execute(
    "INSERT INTO `comments` (`id`, `post_id`, `account_id`, 
     `parent_id`) VALUES (?, ?, ?, ?)",
        (1, 6, 1,None ))
     mydb.commit()    
     mycursor.executemany(
        "INSERT INTO `comments` (`id`, `post_id`, `account_id`, `parent_id`) VALUES (?, ?, ?, ?)",
        [ (2, 6, 2, 1)])    
     mydb.commit()

MySQL

     sql = """INSERT INTO forums (id) VALUES (%s)"""
     val = [("1",), ("2",), ("3",)]
     mycursor.executemany(sql,val)
     mydb.commit()
     mycursor.executemany( 
        "INSERT INTO accounts (id) VALUES (%s)",
        [(1,), (2,), (3,), (4,)]
     )
     mydb.commit()
     mycursor.executemany(
        "INSERT INTO posts (id, forum_id, account_id) VALUES (%s, %s, %s)",
        [(6, 3, 1)]
     )
     mydb.commit()
     mycursor.execute(
        "INSERT INTO `comments` (`id`, `post_id`, `account_id`, `parent_id`) VALUES (%s, %s, %s, %s)",
        (1, 6, 1,None ))
     mydb.commit()    
     mycursor.executemany(
        "INSERT INTO `comments` (`id`, `post_id`, `account_id`, `parent_id`) VALUES (%s, %s, %s, %s)",
        [ (2, 6, 2, 1)])    
     mydb.commit()

6
  • This was my first guess which is why I commented out the lines. The error still appears and im 100% sure that this constraint is not actually there:
    – RobinW
    May 3, 2020 at 22:00
  • as i wrote in my answer, you also have to commit the other insert that are used as reference. In VS code it is simple ti do a one by one debug and it shows you where it quits. i added the whole code that io used on mysql. the forums are now different because i didn't understand how python works in that case
    – nbk
    May 3, 2020 at 22:07
  • I think you misunderstood... the constraint you are referring to never existed. It was my fault to leave it commented out in this example instead of simply removing it from the code. To make it more clear I made a simple counter example and stumbled upon the bug (I think it is one). It seems that executemany() cannot deal with mixed types in one column. If I change my code so that all parent_id entries are either None or some integer it works just fine. This is also the reason why your code works. You separated the different types in that column in two separate statements.
    – RobinW
    May 3, 2020 at 22:26
  • yes that is correct, the connector throws an error that is not resolvable, other than my code. in my database are all the constraints active. I think it is a bug, so you should get back to the developer and report it. I was monizoring my server and it never received any insert at all. then i added the comits so it run till comments there the only solution is that what i showed you.
    – nbk
    May 3, 2020 at 22:38
  • 1
    It was fixed in 0.9.58-beta May 11, 2020 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.