Opened 15 years ago

Closed 15 years ago

#7026 closed (fixed)

Exception message is worded poorly (backwards)

Reported by: ryan@… Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: models many-to-many manytomany m2m exception primary key pk
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


This occurs with recent SVN checkout.
(trimmed) example models:

class Tag(models.Model):
    name = models.CharField(max_length=127, unique=True)
class Image(models.Model):
    tags = models.ManyToManyField(Tag)

code which bombs:

t = Tag(name='foo')
i = Image()
i.tags.add(t) #Bombs here because i doesn't have a pk
  File "", line 73, in ?
  File "/usr/lib/python2.4/site-packages/django/db/models/fields/", line 485, in __get__
  File "/usr/lib/python2.4/site-packages/django/db/models/fields/", line 317, in __init__
    raise ValueError("%r instance needs to have a primary key value before a many-to-many relationship can be used." % model)
ValueError: <class 'eTrain.models.Tag'> instance needs to have a primary key value before a many-to-many relationship can be used.

The error should indicate the <class 'eTrain.models.Image'> instance doesn't have pk... I'm not sure how to get at this as superclass doesn't seem to have the model attribute set.

Change History (2)

comment:1 Changed 15 years ago by Jeff Anderson

Component: UncategorizedDatabase wrapper
Triage Stage: UnreviewedAccepted

I also ran into this error: I was stumped because I knew that what it was saying was wrong.

comment:2 Changed 15 years ago by Adrian Holovaty

Resolution: fixed
Status: newclosed

(In [7622]) Fixed #7026 -- Fixed misleading/incorrect exception text when adding to a many-to-many set on an object that doesn't yet have a primary-key value. Thanks for the report, ryan@…

Note: See TracTickets for help on using tickets.
Back to Top