gpt4 book ai didi

python - 错误 : trying to redefine a primary key as non-primary key

转载 作者:行者123 更新时间:2023-11-29 12:54:19 28 4
gpt4 key购买 nike

我正在使用 dataset尝试将 postgres 数据库备份到 sqlite 文件中的库。我正在运行的代码如下:

local_db = "sqlite:///backup_file.db"

with dataset.connect(local_db) as save_to:
with dataset.connect(postgres_db) as download_from:

for row in download_from['outlook']:
save_to['outlook'].insert(row)

如果我打印表格的一行,它看起来像这样:

OrderedDict([
('id', 4400),
('first_sighting', '2014-08-31'),
('route', None),
('sighted_by', None),
('date', None)
])

但是,当我到达 save_to['outlook'].insert(row) 行时,我收到以下堆栈跟踪错误:

Traceback (most recent call last):
File "/home/anton/Development/Python/TTC/backup_db.py", line 25, in <module>
save_to['outlook'].insert(dict(row))
File "/home/anton/.virtualenvs/flexity/lib/python3.6/site-packages/dataset/table.py", line 79, in insert
row = self._sync_columns(row, ensure, types=types)
File "/home/anton/.virtualenvs/flexity/lib/python3.6/site-packages/dataset/table.py", line 278, in _sync_columns
self._sync_table(sync_columns)
File "/home/anton/.virtualenvs/flexity/lib/python3.6/site-packages/dataset/table.py", line 245, in _sync_table
self._table.append_column(column)
File "/home/anton/.virtualenvs/flexity/lib/python3.6/site-packages/sqlalchemy/sql/schema.py", line 681, in append_column
column._set_parent_with_dispatch(self)
File "/home/anton/.virtualenvs/flexity/lib/python3.6/site-packages/sqlalchemy/sql/base.py", line 431, in _set_parent_with_dispatch
self._set_parent(parent)
File "/home/anton/.virtualenvs/flexity/lib/python3.6/site-packages/sqlalchemy/sql/schema.py", line 1344, in _set_parent
self.key, table.fullname))
sqlalchemy.exc.ArgumentError: Trying to redefine primary-key column 'id' as a non-primary-key column on table 'outlook'

关于我做错了什么有什么想法吗?我在 python 2.7.14 和 3.6.3 中试过这个

最佳答案

假设你有一个为“outlook”制作的模式和表格,你是否制作了一个PK字段?有没有让sqlite决定哪个字段做PK字段?

您尝试插入 id 两次是非常重要的。有一次,sqlite是自己插入的,其他的来自其他表记录。

关于python - 错误 : trying to redefine a primary key as non-primary key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47127099/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com