Forums

Error with migrating from Sqlite to MySQL Database

Hi Everyone,

Goal: Migrate my existing sqlite db to mysql.

What I did (based on other peoples work here in the forum):

  1. python manage.py dumpdata > datadump.json
  2. Create password on database tab
  3. Update Settings.py
  4. python manage.py migrate
  5. python manage.py loaddata datadump.json

Problem: The problem occured at the last step. It seems it worked ok for my other models. Error:

ValueError: The database backend does not accept 0 as a value for AutoField.

My Thoughts: So after reading about the error, the reason might be that some of my foreignkeys have used 0 as default. How do I solve this? Help would be much appreciated.

Full Traceback Below:

Running migrations:
Applying workforce.0001_initial... OK
Applying workforce.0002_auto_20181013_1630... OK
Applying workforce.0003_auto_20181014_0151... OK
Applying workforce.0004_employeeprofile... OK
Applying workforce.0005_accounts... OK
Applying workforce.0006_family... OK
Applying workforce.0007_workhistory... OK
Applying workforce.0008_projectsite... OK
Applying workforce.0009_auto_20181015_0646... OK
Applying workforce.0010_auto_20181015_0749... OK
Applying workforce.0011_auto_20181015_0810... OK
Applying workforce.0012_auto_20181022_0522... OK
Applying workforce.0013_auto_20181025_0454... OK
Applying workforce.0014_auto_20181028_1141... OK
Applying workforce.0015_leave... OK
Applying workforce.0016_auto_20181110_0832... OK
Applying workforce.0017_auto_20181110_0838... OK
Applying workforce.0018_auto_20181127_1024... OK
Applying workforce.0019_filedocument... OK
Applying workforce.0020_auto_20181220_1501... OK
Applying workforce.0021_auto_20181220_1504... OK
Applying workforce.0022_auto_20181220_1517... OK
Applying workforce.0023_education... OK
Applying workforce.0024_auto_20190122_1210... OK
Applying workforce.0025_auto_20190122_1214... OK
Applying workforce.0026_auto_20190202_1316... OK
Applying workforce.0027_auto_20190202_1331... OK
Applying workforce.0028_attachment... OK
Applying workforce.0029_attachment_base_profile... OK
Applying workforce.0030_auto_20190202_1352... OK
Applying workforce.0031_auto_20190202_1446... OK
Applying workforce.0032_auto_20190207_1043... OK
Applying workforce.0033_auto_20190207_1137... OK
Applying workforce.0034_auto_20190207_1140... OK
Applying workforce.0035_auto_20190208_1132... OK
Applying workforce.0036_auto_20190208_1135... OK
Applying workforce.0037_auto_20190208_1139... OK
Applying workforce.0038_auto_20190208_1142... OK
Applying workforce.0039_auto_20190208_1146... OK
Applying workforce.0040_auto_20190208_1201... OK
Applying workforce.0041_auto_20190214_1504... OK
Applying workforce.0042_auto_20190216_1038... OK
Applying workforce.0043_auto_20190216_1046... OK
Applying workforce.0044_auto_20190216_1049... OK
Applying workforce.0045_auto_20190218_0149... OK
Applying workforce.0046_auto_20190218_2041... OK
Applying workforce.0047_leave_tots_days... OK
Applying workforce.0048_auto_20190223_1641... OK
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying purchase.0001_initial... OK
Applying purchase.0002_itemorder_orderrequest_purchasecost... OK
Applying purchase.0003_auto_20190116_0901... OK
Applying purchase.0004_orderrequest_create_date... OK
Applying purchase.0005_auto_20190117_1334... OK
Applying purchase.0006_auto_20190117_1416... OK
Applying purchase.0007_auto_20190117_1440... OK
Applying purchase.0008_auto_20190117_1442... OK
Applying purchase.0009_orderrequest_details... OK
Applying purchase.0010_orderrequest_attachment... OK
Applying purchase.0011_auto_20190119_1926... OK
Applying purchase.0012_auto_20190119_2016... OK
Applying purchase.0013_remove_orderrequest_attachment... OK
Applying purchase.0014_auto_20190123_1109... OK
Applying purchase.0015_auto_20190123_1124... OK
Applying purchase.0016_auto_20190123_1126... OK
Applying purchase.0017_inventory... OK
Applying purchase.0018_itempurchase_purchase_date... OK
Applying purchase.0019_auto_20190124_1301... OK
Applying purchase.0020_auto_20190124_1525... OK
Applying purchase.0021_auto_20190125_0010... OK
Applying accounting.0001_initial... OK
Applying accounting.0002_remove_liquidation_form_no... OK
Applying accounting.0003_remove_liquidation_trans_type... OK
Applying accounting.0004_remove_liquidation_issued_to... OK
Applying accounting.0005_auto_20190201_0134... OK
Applying accounting.0006_auto_20190201_0136... OK
Applying accounting.0007_auto_20190202_1310... OK
Applying accounting.0008_auto_20190202_1446... OK
Applying accounting.0009_auto_20190204_1033... OK
Applying accounting.0010_auto_20190204_1038... OK
Applying accounting.0011_auto_20190204_1740... OK
Applying accounting.0012_auto_20190207_1249... OK
Applying accounting.0013_cashbudget_cdv_no... OK
Applying accounting.0014_auto_20190207_1453... OK
Applying accounting.0015_liquidation_vat... OK
Applying accounting.0016_auto_20190208_0027... OK
Applying accounting.0017_auto_20190208_0028... OK
Applying accounting.0018_auto_20190208_0039... OK
Applying accounting.0019_auto_20190208_0126... OK
Applying accounting.0020_cashbudget_doc_file... OK
Applying accounting.0021_auto_20190208_1602... OK
Applying accounting.0022_auto_20190208_1603... OK
Applying accounting.0023_auto_20190208_1620...Traceback (most recent call last):
File "manage.py", line 15, in <module>
  execute_from_command_line(sys.argv)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/core/management/__init__.py", 
line 381, in execute_from_command_line
  utility.execute()
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/core/management/__init__.py", 
line 375, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
Applying workforce.0014_auto_20181028_1141... OK
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/core/management/base.py", 
line 316, in run_from_argv
  self.execute(*args, **cmd_options)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/core/management/base.py", 
line 353, in execute
  output = self.handle(*args, **options)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/core/management/base.py", 
line 83, in wrapped
  res = handle_func(*args, **kwargs)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site- 
packages/django/core/management/commands/m
igrate.py", line 203, in handle
  fake_initial=fake_initial,
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/db/migrations/executor.py"
, line 117, in migrate
  state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/db/migrations/executor.py"
, line 147, in _migrate_all_forwards
  state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/db/migrations/executor.py"
, line 244, in apply_migration
  state = migration.apply(state, schema_editor)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/db/migrations/migration.py
", line 124, in apply
  operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/db/migrations/operations/f
ields.py", line 216, in database_forwards
  schema_editor.alter_field(from_model, from_field, to_field)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/db/backends/base/schema.py
", line 523, in alter_field
  old_db_params, new_db_params, strict)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/db/backends/base/schema.py
", line 627, in _alter_field
  new_default = self.effective_default(new_field)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/db/backends/base/schema.py
", line 239, in effective_default
  return field.get_db_prep_save(default, self.connection)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/db/models/fields/related.p
y", line 937, in get_db_prep_save
  return self.target_field.get_db_prep_save(value, connection=connection)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/db/models/fields/__init__.
py", line 790, in get_db_prep_save
  return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/db/models/fields/__init__.
py", line 957, in get_db_prep_value
  value = connection.ops.validate_autopk_value(value)
File "/home/eakdev/.virtualenvs/aip_env/lib/python3.6/site-packages/django/db/backends/mysql/operatio
ns.py", line 172, in validate_autopk_value
  raise ValueError('The database backend does not accept 0 as a '
ValueError: The database backend does not accept 0 as a value for AutoField.

Seems like it is this? In particular the accounting.0023_auto_20190208_1620 migration that it failed on.

Hi,

Yes I saw that, what I did is to change the default=0 to None, then did a migration and it seems to be working. still migrating though, hope I don't encounter any other errors.

Thanks!!