python - How can I configure MYSQL DB to accept NULL for primary key in the INSERT statement and then generate unique primary key
问题描述
I am using the code below from this stackoverflow answer to copy a row in a table and create a new row in the same table, but with the same data and a unique primary key. I am running the code using the mysql-connector-python module.
CREATE TEMPORARY TABLE tmptable_1 SELECT * FROM table WHERE primarykey = 1;
UPDATE tmptable_1 SET primarykey = NULL;
INSERT INTO table SELECT * FROM tmptable_1;
DROP TEMPORARY TABLE IF EXISTS tmptable_1;
This code runs on the production database, but gives the following error on my local test db.
IntegrityError: 1048 (23000): Column 'primaryKey' cannot be null
Local DB:
- Version:
MYSQL==5.6.47
- Compiled For: osx10.15 (x86_64)
- DB Client:
mysql-connector-python==2.1.3
Production DB:
- Version:
MYSQL==5.6.40
- Compiled For: Linux (x86_64)
- DB Client:
mysql-connector-python==2.1.3
I use MYSQL Workbench to export the schema and data from the production data base and import it into my local test database.
Is there some configuration setting for a MYSQL DB that is the problem? I am not sure what the cause is for the difference in database behaviour.
解决方案
只需从INSERT
侧面放下该列,您就可以插入自己:
INSERT INTO table (a, b, c) SELECT a, b, c FROM table;
您必须以完全相同的顺序指定列,省略id
一个。
推荐阅读
- angular - 使用 Angular 11 将我的 REST API(Spring boot)中的事件读入我的完整日历
- json - 角材料未对表中的数字进行排序
- c# - 通过 Windows 服务 C# .net 中的 zkemkeeper.dll 连接到设备
- javascript - 突出显示焦点上的文本 - 剑道组合框
- sql-server - 在 Microsoft SQL 中是否有一个现有的存储过程来列出架构的所有表?
- php - ID 未保存到数据库
- wpf - 在 Avalon dock 中,LayoutAnchorable WPF 应用程序没有出现 Title 和 Dock 组合按钮选项
- abap - 显示两个用户的角色?
- python-3.x - 在 sympy 中替换具有不确定性的值
- r - 如何减少 ggplot2 bquote 标签标题中 unicode 字符之间的空格?