错误:尝试将主键重新定义为非主键

时间:2017-11-05 22:00:18

标签: python postgresql sqlite

我正在使用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

中尝试过这个

2 个答案:

答案 0 :(得分:1)

假设你有一个为“outlook”制作的架构和表格,你是否制作了PK字段?您是否让sqlite决定在哪个字段中创建PK字段?

你试图两次插入id是非常高的。有一次,sqlite正在插入自己,而其他来自其他表记录。

答案 1 :(得分:1)

我明白了!因此,诀窍是默认情况下database库使表格具有自动递增的整数主键。但是,我的数据已经有一个'id'列。为了避免这个问题,我应该在尝试向其添加行之前定义我的表,并在没有主键的情况下定义它,如下所示:

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

        table_to_save_to = save_to.create_table('outlook', primary_id=False)

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

通过.create_table(table_name, primary_key=False)我可以确保我可以将自己的id值插入表中。

我通过reading the docs找到了这个解决方案。