Error: trying to redefine a primary key as non-primary key

Error: trying to redefine a primary key as non-primary key

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

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

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

中试过了

假设您有一个模式并且 table 为 "outlook" 制作,您是否制作了 PK 字段?你让sqlite决定哪个字段做PK字段吗?

您尝试插入 id 两次是非常严重的。一次,sqlite在插入自己,其他来自其他table条记录。

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

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 值插入 table。

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