Django 2.0 SQLite3到MySQL loaddata错误:“数据库后端不接受0作为AutoField的值。”

时间:2018-03-08 02:35:37

标签: mysql django sqlite loaddata dumpdata

我正在尝试将数据库从sqlite传输到mysql。

我已经用Google搜索了这个错误,发现Stack Overflow匹配,但还没有看到如何调试/识别有问题的“0值AutoField”字段。我试过通过转储/加载不同的表来避开问题,但似乎都产生了同样的错误。

我尝试将-e contenttypes--natural-foreign--natural-primary附加到我的datadump命令,例如,

python manage.py dumpdata -e contenttypes --natural-foreign --natural-primary --indent=4 > datadump_3-7-18.json

运行python manage.py loaddata --traceback datadump_3-7-18.json

之后

它产生回溯错误:

(venv) ➜  bikerental git:(additional-features-march) ✗ python manage.py loaddata --traceback datadump_3-7-18.json
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/rentals/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/rentals/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/rentals/venv/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/rentals/venv/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/rentals/venv/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
    self.loaddata(fixture_labels)
  File "/rentals/venv/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 113, in loaddata
    self.load_label(fixture_label)
  File "/rentals/venv/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 177, in load_label
    obj.save(using=self.using)
  File "/rentals/venv/lib/python3.6/site-packages/django/core/serializers/base.py", line 205, in save
    models.Model.save_base(self.object, using=using, raw=True, **kwargs)
  File "/rentals/venv/lib/python3.6/site-packages/django/db/models/base.py", line 759, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/rentals/venv/lib/python3.6/site-packages/django/db/models/base.py", line 842, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/rentals/venv/lib/python3.6/site-packages/django/db/models/base.py", line 880, in _do_insert
    using=using, raw=raw)
  File "/rentals/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/rentals/venv/lib/python3.6/site-packages/django/db/models/query.py", line 1125, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/rentals/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1280, in execute_sql
    for sql, params in self.as_sql():
  File "/rentals/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1233, in as_sql
    for obj in self.query.objs
  File "/rentals/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1233, in <listcomp>
    for obj in self.query.objs
  File "/rentals/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1232, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "/rentals/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1172, in prepare_value
    value = field.get_db_prep_save(value, connection=self.connection)
  File "/rentals/venv/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 767, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/rentals/venv/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 940, in get_db_prep_value
    value = connection.ops.validate_autopk_value(value)
  File "/rentals/venv/lib/python3.6/site-packages/django/db/backends/mysql/operations.py", line 163, in validate_autopk_value
    raise ValueError('The database backend does not accept 0 as a '
ValueError: Problem installing fixture '/rentals/bikerental/datadump_3-7-18.json': The database backend does not accept 0 as a value for AutoField.

我注意到这似乎与Foreignkey值有关,所以我会发布我的那个:

bike = models.ManyToManyField(Bike, blank=True)

有什么方法可以更容易地识别数据库中的哪个位置,我认为它来自哪里?

1 个答案:

答案 0 :(得分:2)

我通过手动编辑数据转储来解决问题。在ManyToManyField引用的表中,有一条ID为 0 的记录。这是由于我最初手动输入的记录集,我在0处开始增量。删除此记录,并在ManyToManyField中删除对它的引用,允许loaddata命令无错误地工作。为了记录,我想说明错误处理可以/应该通过更明确的改进来改进,因为它让我在整整一个工作日都摸不着头脑。

有关datadump_3-7-18.json的说明:

{                                 <----- I GOT DELETED  
    "model": "inventory.bike",    <----- I GOT DELETED  
    "pk": 0,                      <----- I GOT DELETED  
    "fields": {                   <----- I GOT DELETED   
       ...                        <----- I GOT DELETED  
    }                             <----- I GOT DELETED  
},                                <----- I GOT DELETED  
{
    "model": "inventory.bike",
    "pk": 1,
    "fields": {
       ...
    }
},
{
    "model": "inventory.bike",
    "pk": 2,
    "fields": {
       ...
    }
},

稍后在datadump_3-7-18.json中,包含0 ManyToManyField外键的记录:

{
    "model": "reservations.reservation",
    "pk": 55,
    "fields": {
       ...
        "bike": [
            0,      <----- I GOT DELETED   
            1,
            2
        ]
    }
},
{
    "model": "reservations.reservation",
    "pk": 28,
    "fields": {
        ...
        "bike": [
            0,      <----- I GOT DELETED  
            1,
            2,
            3
        ]
    }
},