I am working with some legacy data and need to preserve the existing record ID's which, in some tables, include the value of 0 for their auto-increment field.
I can check that this works by running the following test inside the MySQL shell:
create table number (number int not null auto_increment primary key); insert into number(number) values (0); select * from number; drop table number;
+--------+
| number |
+--------+
| 0 |
+--------+
1 row in set (0,00 sec)
However, when I try this approach with one of my existing models (in the Django shell) I get an error:
from myapp.models import MyModel
MyModel.objects.create(pk=0, direction=1, code='UNK')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/gamesbook/,virtualenvs/proj/local/lib/python2.7/site-packages/django/db/models/manager.py", line 157, in create
return self.get_queryset().create(**kwargs)
File "/home/gamesbook/,virtualenvs/proj/local/lib/python2.7/site-packages/django/db/models/query.py", line 322, in create
obj.save(force_insert=True, using=self.db)
File "/home/gamesbook/,virtualenvs/proj/local/lib/python2.7/site-packages/django/db/models/base.py", line 545, in save
force_update=force_update, update_fields=update_fields)
File "/home/gamesbook/,virtualenvs/proj/local/lib/python2.7/site-packages/django/db/models/base.py", line 573, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/gamesbook/,virtualenvs/proj/local/lib/python2.7/site-packages/django/db/models/base.py", line 654, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/gamesbook/,virtualenvs/proj/local/lib/python2.7/site-packages/django/db/models/base.py", line 687, in _do_insert
using=using, raw=raw)
File "/home/gamesbook/,virtualenvs/proj/local/lib/python2.7/site-packages/django/db/models/manager.py", line 232, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/home/gamesbook/,virtualenvs/proj/local/lib/python2.7/site-packages/django/db/models/query.py", line 1514, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/gamesbook/,virtualenvs/proj/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 902, in execute_sql
for sql, params in self.as_sql():
File "/home/gamesbook/,virtualenvs/proj/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 860, in as_sql
for obj in self.query.objs
File "/home/gamesbook/,virtualenvs/proj/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 350, in get_db_prep_save
prepared=False)
File "/home/gamesbook/,virtualenvs/proj/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 607, in get_db_prep_value
value = connection.ops.validate_autopk_value(value)
File "/home/gamesbook/,virtualenvs/proj/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 336, 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.
Is there a setting I need to change such that Django does not raise this error and allows the PK to be zero?
Thanks
Derek
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAF1Wu3OFYU3jKjAPNTqk5DisJNGGiOr5YZiQBWBv0FTLQH87Qg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment