Code

Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#12883 closed (duplicate)

Adding values in a m2m field breaks (_state.db is None instead of "default")

Reported by: IonelMaries Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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.

Attachments (0)

Change History (5)

comment:1 Changed 4 years ago by semenov

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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?

comment:2 Changed 4 years ago by russellm

  • milestone set to 1.2
  • Resolution set to invalid
  • Status changed from new to 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 Changed 4 years ago by ext

  • Resolution invalid deleted
  • Status changed from closed to 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 Changed 4 years ago by russellm

  • Resolution set to duplicate
  • Status changed from reopened to 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.

comment:5 Changed 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.