django-nonrel on google app engine: Can PrimaryKey fields have a value of 0?
In trying to bulk load some legacy data from a django project that I've been converting from MS-Access to Google App Engine (via django-nonrel), I think I've hit a brick wall when trying to insert data with a primary key with a 0 (zero) value. Many of my data have zero primary keys, and the rows with zero primary keys are treated as special and referenced by many foreign keys (thereby making it very difficult to fix all of the references to rows with .
I get the following error upon trying to insert it:
File "/Users/myuser/my_app/django/core/management/commands/loaddata.py", line 174, in handle
obj.save(using=using)
File "/Users/myuser/my_app/django/core/serializers/base.py", line 165, in save
models.Model.save_base(self.object, using=using, raw=True)
File "/Users/myuser/my_app/django/db/models/base.py", line 573, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
File "/Users/myuser/my_app/django/db/models/manager.py", line 195, in _insert
return insert_query(self.model, values, **kwargs)
File "/Users/myuser/my_app/django/db/models/query.py", line 1438, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/myuser/my_app/dbindexer/compiler.py", line 38, in execute_sql
return super(SQLInsertCompiler, self).execute_sql(return_id=return_id)
File "/Users/myuser/my_app/djangotoolbox/db/basecompiler.py", line 370, in execute_sql
return self.insert(data, return_id=return_i开发者_JS百科d)
File "/Users/myuser/my_app/djangoappengine/db/compiler.py", line 59, in _func
return func(*args, **kwargs)
File "/Users/myuser/my_app/djangoappengine/db/compiler.py", line 470, in insert
entity = Entity(self.query.get_meta().db_table, **kwds)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/datastore.py", line 784, in __init__
datastore_types.ValidateInteger(id, 'id')
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/datastore_types.py", line 195, in ValidateInteger
raise exception('%s must not be 0 (zero)' % name)
DatabaseError: id must not be 0 (zero)
From this dump you can see that the error is being raised in the GoogleAppEngine python api code itself, rather than the django-nonrel or djangoappengine code. So my question is, is there a preference you can set to allow zero values in integer primary key fields in google app engine?
Thanks!
I had a similar problem migrating from mysql to google app engine. A solution could be to set the integer value to negative if you can't use 0 and positives are reserved. I set my primary keys from import to negative and could work from there by treating imports and plain gae data different ways.
The error seems pretty clear: No, you can't set the primary (integer) key to 0.
精彩评论