Opened 14 years ago
Closed 10 years ago
#14003 closed Bug (fixed)
Filtering on a relation whose model has a different PK type doesn't work
Reported by: | David Gouldin | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.2 |
Severity: | Normal | Keywords: | |
Cc: | dgouldin@… | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Using the following models:
class Author(models.Model): name = models.CharField(max_length=255) class Book(models.Model): name = models.CharField(max_length=50, primary_key=True) author = models.ForeignKey(Author)
The following code fails:
In [1]: a = Author.objects.create(name='Me') In [2]: b = Book.objects.create(name='My Book', author=a) In [3]: Author.objects.filter(book=b) .... ValueError: invalid literal for int() with base 10: 'My Book'
This seems to be the source of the problem:
_pk_trace uses field.rel.to to resolve the field to use for prepping the filter value. In the case above, It's preparing Book.pk (a CharField) as if it were a value for Author.pk (an AutoField) and blows up when casting the string to an int. I'm not sure how best to go about solving the issue. _pk_trace doesn't currently have any way I know of to tell which side of the relation the value arg is coming from.
Attachments (1)
Change History (7)
comment:1 by , 14 years ago
Cc: | added |
---|
comment:2 by , 14 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
I believe this is the same root problem as what is described in #11319.
comment:3 by , 13 years ago
Easy pickings: | unset |
---|---|
Resolution: | duplicate |
Severity: | → Normal |
Status: | closed → reopened |
Type: | → Bug |
UI/UX: | unset |
The fix for 11319 unfortunately didn't fix this. :( I have this problem 1.2.7 and 1.3.1. This is a regression from 1.1.1 which works fine.
by , 13 years ago
Attachment: | 14003-testcase.patch added |
---|
comment:4 by , 13 years ago
Triage Stage: | Unreviewed → Accepted |
---|
I can confirm the problem. I'm attaching a test case. The tests fails with trunk; but if I remove , foreign_key=True
in the definition of the Book
model, it passes.
comment:5 by , 12 years ago
Status: | reopened → new |
---|
comment:6 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Look's like this was fixed in 97774429aeb54df4c09895c07cd1b09e70201f7d
I actually found this while researching a fix for #13383. I don't think that ticket can be properly fixed until this one is resolved.