简体   繁体   English

PRIMARY KEY 必须是唯一的

[英]PRIMARY KEY must be unique

I am trying to write some data to a table in a database which I am creating.我正在尝试将一些数据写入我正在创建的数据库中的表中。 However, I am facing with an integrity error like:但是,我面临着完整性错误,例如:

sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) PRIMARY KEY must be unique sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) PRIMARY KEY 必须是唯一的

My question is how to avoid these errors as I will run a couple of times the script我的问题是如何避免这些错误,因为我将运行几次脚本

Basically you are creating an object with an already existing primary key, and it's not accepted by SQLite.基本上,您正在使用已经存在的主键创建一个对象,并且 SQLite 不接受它。 Verify it by querying the db with something like通过使用类似的查询数据库来验证它

select * from airport where id = 6256

If the query returns a result, you need to change the id of the airport you are saving.如果查询返回结果,则需要更改您保存的机场的 id。 Since you use the autoincrement, you don't need to specify an id and the DBMS will assign the next free id in that table.由于您使用自动增量,因此您无需指定 id,DBMS 将分配该表中的下一个空闲 id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM