0

In the following scenario, what I expected is an exception, but I got a warning:

# MariaDB: 10

create table tm
(
    id int auto_increment primary key,
    col1 int not null
) ENGINE = InnoDB;

My script:

# Python: 3.6.1
# PyMySQL: 0.7.11

import pymysql


def main():
    connection = None
    try:
        connection = pymysql.connect(host='localhost',
                                     port=3306,
                                     user='username',
                                     password='password',
                                     db='dev')

        with connection.cursor() as cursor:
            sql = 'INSERT INTO `tm` (`tm`.`id`) VALUES (DEFAULT)'
            cursor.execute(sql)
        connection.commit()

    finally:
        connection.close()


if __name__ == '__main__':
    main()

In the shell:

$ ./main.py
.../venv/lib/python3.6/site-packages/pymysql/cursors.py:166: Warning: (1364, "Field 'col1' doesn't have a default value")
  result = self._query(query)

After the execution, a record with col1 equal to 0 was added to the database. Then, I executed the query directly in the MariaDB console. I got [HY000][1364] Field 'col1' doesn't have a default value, and no record was inserted.

I checked the SQL_MODE by SELECT @@GLOBAL.sql_mode global, @@SESSION.sql_mode session and got:

  global                                     session
1 NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

How did it happen in the engine InnoDB?

chehsunliu
  • 1,559
  • 1
  • 12
  • 22

0 Answers0