#18652 closed Bug (invalid)
loaddata --database option ignored
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Core (Management commands) | Version: | 1.4 |
Severity: | Normal | Keywords: | loaddata --database manage.py |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When using the manage.py loaddata command, the --database option appears to be silently ignored. Instead of applying to the specified database, it applies to the default database.
Example follows:
settings.py
DATABASES = { 'mysql': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'django', 'USER': 'django', 'PASSWORD': 'XXXXXXXXXXXX', 'HOST': '', 'PORT': '', }, 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': '/foo.db', } }
commands used
foo.db contains some data.
./manage.py flush --database=mysql
You have requested a flush of the database. This will IRREVERSIBLY DESTROY all data currently in the 'django' database, and return each table to the state it was in after syncdb. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: yes You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no): no Installed 0 object(s) from 0 fixture(s)
mysql database inspected - verified it contains no data at this point
./manage.py dumpdata -a -n > data.json
data.json inspected - contains sample data from the default database (foo.db)
./manage.py loaddata --database=mysql ./data.json
Problem installing fixture './data.json': Traceback (most recent call last): File "/usr/local/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 196, in handle obj.save(using=using) File "/usr/local/lib/python2.6/site-packages/django/core/serializers/base.py", line 165, in save models.Model.save_base(self.object, using=using, raw=True) File "/usr/local/lib/python2.6/site-packages/django/db/models/base.py", line 565, in save_base created=(not record_exists), raw=raw, using=using) File "/usr/local/lib/python2.6/site-packages/django/dispatch/dispatcher.py", line 172, in send response = receiver(signal=self, sender=sender, **named) File "/genpool0/db/foo/intranet/../intranet/userprofile/models.py", line 22, in create_user_profile UserProfile.objects.create(user=instance) File "/usr/local/lib/python2.6/site-packages/django/db/models/manager.py", line 137, in create return self.get_query_set().create(**kwargs) File "/usr/local/lib/python2.6/site-packages/django/db/models/query.py", line 377, in create obj.save(force_insert=True, using=self.db) File "/usr/local/lib/python2.6/site-packages/django/db/models/base.py", line 463, in save self.save_base(using=using, force_insert=force_insert, force_update=force_update) File "/usr/local/lib/python2.6/site-packages/django/db/models/base.py", line 551, in save_base result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw) File "/usr/local/lib/python2.6/site-packages/django/db/models/manager.py", line 203, in _insert return insert_query(self.model, objs, fields, **kwargs) File "/usr/local/lib/python2.6/site-packages/django/db/models/query.py", line 1576, in insert_query return query.get_compiler(using=using).execute_sql(return_id) File "/usr/local/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 910, in execute_sql cursor.execute(sql, params) File "/usr/local/lib/python2.6/site-packages/django/db/backends/util.py", line 40, in execute return self.cursor.execute(sql, params) File "/usr/local/lib/python2.6/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute return Database.Cursor.execute(self, query, params) IntegrityError: Could not load auth.User(pk=1): column user_id is not unique
note the 3rd to last line of the traceback. it's using sqlite3 backend instead of mysql.
let's try flushing the default database
./manage.py flush
You have requested a flush of the database. This will IRREVERSIBLY DESTROY all data currently in the '/genpool0/db/foo/foo.db' database, and return each table to the state it was in after syncdb. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: yes You just installed Django's auth system, which means you don't have any superusers defined. Would you like to create one now? (yes/no): no Installed 0 object(s) from 0 fixture(s)
and try the same command that failed before
./manage.py loaddata --database=mysql ./data.json
Installed 1570 object(s) from 1 fixture(s)
the data was installed back into the default database, instead of the mysql database
Change History (3)
comment:1 by , 12 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 12 years ago
You are absolutely correct - my bad. Userprofile was created in custom code like this:
def create_user_profile(sender,instance,created,**kwargs): if created: UserProfile.objects.create(user=instance) post_save.connect(create_user_profile,sender=User,dispatch_uid="userprofile")
comment:3 by , 12 years ago
It might be a good idea to add a note to the doucmentation at https://docs.djangoproject.com/en/dev/topics/auth/ and/or update the example code to something like this:
def create_user_profile(sender,instance,created,**kwargs): if created: UserProfile.objects.using(kwargs['using']).create(user=instance) post_save.connect(create_user_profile,sender=User,dispatch_uid="userprofile")
I think it has nothing to do with the loaddata database. It's related to this line:
In custom code, you are creating a user profile object without specifiying the database, hence it will always use the default database. If you want the user profile being created in the same database as the user, you'll have to add some code to do that specifically in your userprofile app.