#12883 closed (duplicate)
Adding values in a m2m field breaks (_state.db is None instead of "default")
Reported by: | Ionel Cristian Maries | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Traceback (most recent call last): File "x.py", line 123, in process_release bla.somem2mfield.add(foo) File "/home/snip/ve/src/django/django/db/models/fields/related.py", line 465, in add self._add_items(self.source_field_name, self.target_field_name, *objs) File "/home/snip/ve/src/django/django/db/models/fields/related.py", line 525, in _add_items (obj, self.instance._state.db, obj._state.db)) ValueError: Cannot add "<Foo: Foo object>": instance is on database "None", value is is on database "default"
I don't understand why this happens. I don't have any explicit "using" anywhere in my code.
Change History (5)
comment:1 by , 15 years ago
comment:2 by , 15 years ago
milestone: | → 1.2 |
---|---|
Resolution: | → invalid |
Status: | new → closed |
This is a concerning report because it covers an area of new functionality -- but as @semenov says, there's nowhere near enough detail to replicate the problem.
Marking Invalid due to the lack of info. If you can provide a full test case (models and the sequence of calls that leads to the problem) please reopen. Alternatively, ask on django-users to confirm that you have correct usage.
comment:3 by , 15 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
I'm reopening, because, I've got the same problem.
Code causing the problem is:
(...) project = form.save() admin_profiles = Member.objects.select_related().filter(group__name='Admin') for admin_profile in admin_profiles: project.users.add(admin_profile.user)
It is just adding users to a project.users which is m2m to auth.User.
Error report is:
Traceback (most recent call last): File "/var/www/django/someapp/parts/django/django/core/handlers/ base.py", line 101, in get_response response = callback(request, *callback_args, **callback_kwargs) File "/var/www/django/someapp/webshare/utils.py", line 21, in wrapper context = view_func(*args, **kwargs) File "/var/www/django/someapp/webshare/apps/projects/views.py", line 100, in add_project project.users.add(admin_profile.user) File "/var/www/django/someapp/parts/django/django/db/models/fields/ related.py", line 465, in add self._add_items(self.source_field_name, self.target_field_name, *objs) File "/var/www/django/someapp/parts/django/django/db/models/fields/ related.py", line 525, in _add_items (obj, self.instance._state.db, obj._state.db)) ValueError: Cannot add "<User: XXX>": instance is on database "default", value is is on database "None"
It is actually very strange. After I had used pdb and went through the
code slowly there were no error! However during normal execution there
is still a problem.
I found that something goes wrong inside
allow_relation method in django.db.utils. Last line of this function
is:
return obj1._state.db == obj2._state.db
But when I print these variables i get:
without pdb:
- obj1._state.db: None
- obj2._state.db: default
with pdb (I mean step by step execution):
- obj1._state.db: default
- obj2._state.db: default
Also see discussion here: http://groups.google.pl/group/django-users/browse_thread/thread/a81d6618fce29436?pli=1
comment:4 by , 15 years ago
Resolution: | → duplicate |
---|---|
Status: | reopened → closed |
I believe this is actually a duplicate of #13003. The issue is the select_related call not populating the _state.db of the related user object, which then causes the m2m assignment to fail.
IonelMaries, do you realize that in order to fix a bug, it needs to be repeated and confirmed first? How do you think anyone would repeat the bug based solely on your description?