Opened 15 years ago

Closed 15 years ago

#10892 closed (wontfix)

ManyToManyField attribute looks up wrong name in Admin

Reported by: jhawk28@… Owned by: nobody
Component: contrib.admin Version: dev
Severity: Keywords: ManyToManyField
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

ManyToManyField attribute does not look up the correct key in the Admin if the django naming conventions are not followed in a legacy database.

This returns an error like:
ORA-00904: "AUTH_BOOK_MAPPING"."BOOK_ID": invalid identifier
Example:

class Book(models.Model):
  book = models.CharField(max_length=4, primary_key=True, db_column='BOOK')
  description = models.CharField(max_length=250, db_column='DESCRIPTION', blank=True)
  class Meta:
    db_table = u'BOOK_TABLE'

class Author(models.Model):
  auth = models.DecimalField(decimal_places=0, max_digits=38, primary_key=True, db_column='AUTH_ID')
  name = models.CharField(max_length=250, db_column='NAME', blank=True)
  books = models.ManyToManyField(Book, db_table=u'AUTH_BOOK_MAPPING')
  class Meta:
    db_table = u'AUTHOR_TABLE'

Change History (2)

comment:1 by Ramiro Morales, 15 years ago

milestone: 1.1

"AUTH_BOOK_MAPPING"."BOOK_ID is the FK from the intermediate table to the Book model, right?.

I don't think Django can/should introspect the names of these Foreign Keys at runtime. Try creating a model that represent your intermediate table and use Meta.db_table plus the right Foreign Key field names (possibly also using te db_column field option in a way similar to what you 've already done in the Book and Author models). Specifiy that model in the through option for the ManyToManyField.

comment:2 by Alex Gaynor, 15 years ago

Resolution: wontfix
Status: newclosed

Ramiro's analysis is correct, if you don't use an intermediary model then django can't guess at the column name.

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