Opened 17 years ago

Closed 16 years ago

Last modified 13 years ago

#7042 closed (fixed)

Admin Interface generates malformed SQL with many-to-many fields

Reported by: alexander.hungenberg@… Owned by: Charlie La Mothe
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: aug22sprint
Cc: charlie@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I am using latest Revision (7438)

class Teacher(models.Model):
    short = models.CharField(max_length=20)
    account = models.ForeignKey(User, unique=True, blank=True, null=True)
    
    def __unicode__(self):
        if self.account:
            return self.account.last_name + ", " + self.account.first_name
        else:
            return self.short
    
    class Admin:
        list_display = ('account', 'short')

class Class(models.Model):
    name = models.CharField(max_length=3, unique=True)
    teacher = models.ManyToManyField(Teacher, unique=True, blank=True, null=True)
    room = models.ForeignKey(Room, blank=True, null=True)
    enabled_functions = models.ManyToManyField(Function, blank=True, null=True)
    
    def __unicode__(self):
        return self.name
    
    class Admin:
        list_display = ('name', 'room')

When I try to add a Teacher to a Class via the admin-interface, it throws the following Exception:

(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1")

the executed query taken from MySQL Log is

SELECT m2db_class.id,m2db_class.name,m2db_class.room_id FROM m2db_class LEFT OUTER JOIN m2db_class_teacher AS m2m_m2db_class__teacher ON m2db_class.id = m2m_m2db_class__teacher.class_id WHERE (m2m_m2db_class__teacher.teacher_id = ("'1'",))

Other many-to-many fields work normal...

If you need further Infos, contect me by mail or as defreng in #django. I could even give you some access to the admin page

Attachments (1)

t7042_r8472_patch.diff (2.3 KB ) - added by Charlie La Mothe 16 years ago.
Adds model validation to check that unique = True is not set in ManyToManyFields. Also contains invalid_model tests to test for the validation.

Download all attachments as: .zip

Change History (9)

comment:1 by anonymous, 17 years ago

the trigger seems to be the unique=True option in the Class model

comment:2 by Jason Davies, 17 years ago

I can confirm this bug, it seems to occur when unique=True is used in a ManyToManyField.

comment:3 by Eric Holscher, 16 years ago

milestone: 1.0
Triage Stage: UnreviewedAccepted

comment:4 by anonymous, 16 years ago

Owner: changed from nobody to Charlie La Mothe

by Charlie La Mothe, 16 years ago

Attachment: t7042_r8472_patch.diff added

Adds model validation to check that unique = True is not set in ManyToManyFields. Also contains invalid_model tests to test for the validation.

comment:5 by Charlie La Mothe, 16 years ago

Cc: charlie@… added
Component: Admin interfaceDatabase wrapper
Has patch: set
Keywords: aug22sprint added
Resolution: fixed
Status: newclosed

comment:6 by Charlie La Mothe, 16 years ago

Resolution: fixed
Status: closedreopened

comment:7 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: reopenedclosed

(In [8488]) Fixed #7042 -- The management validation command nows alerts users to the
presence (and incorrectness) of unique=True on ManyToManyFields. This has never
worked and generates invalid SQL. Now it's raised as an explicit error during
validation. Thanks to clamothe for the patch.

Still needs a docs change to make this clear, but that goes to the docs
refactor branch.

comment:8 by Jacob, 13 years ago

milestone: 1.0

Milestone 1.0 deleted

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