在以下场景中,我期望的是一个异常,但我收到了警告:
# MariaDB: 10
create table tm
(
id int auto_increment primary key,
col1 int not null
) ENGINE = InnoDB;
我的脚本:
# 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()
在外壳中:
$ ./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)
执行后,col1
数据库中添加了一条等于 0 的记录。然后,我直接在 MariaDB 控制台中执行了查询。我得到了[HY000][1364] Field 'col1' doesn't have a default value
,并且没有插入任何记录。
我查了一下SQL_MODE
,SELECT @@GLOBAL.sql_mode global, @@SESSION.sql_mode session
得到:
global session
1 NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
它是如何在引擎 InnoDB 中发生的?