Django

Code

Ticket #7042 (closed: fixed)

Opened 8 months ago

Last modified 3 months ago

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

Reported by: alexander.hungenberg@gmail.com Assigned to: clamothe
Milestone: 1.0 Component: Database layer (models, ORM)
Version: SVN Keywords: aug22sprint
Cc: charlie@clamothe.com Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

t7042_r8472_patch.diff (2.3 kB) - added by clamothe on 08/22/08 22:26:28.
Adds model validation to check that unique = True is not set in ManyToManyFields?. Also contains invalid_model tests to test for the validation.

Change History

04/22/08 14:32:18 changed by anonymous

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

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

05/31/08 16:19:17 changed by esaj

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

08/08/08 15:52:37 changed by ericholscher

  • stage changed from Unreviewed to Accepted.
  • milestone set to 1.0.

08/22/08 21:58:45 changed by anonymous

  • owner changed from nobody to clamothe.

08/22/08 22:26:28 changed by clamothe

  • 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.

08/22/08 22:28:24 changed by clamothe

  • status changed from new to closed.
  • cc set to charlie@clamothe.com.
  • component changed from Admin interface to Database wrapper.
  • keywords set to aug22sprint.
  • has_patch set to 1.
  • resolution set to fixed.

08/22/08 22:28:39 changed by clamothe

  • status changed from closed to reopened.
  • resolution deleted.

08/23/08 11:56:42 changed by mtredinnick

  • status changed from reopened to closed.
  • resolution set to fixed.

(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.


Add/Change #7042 (Admin Interface generates malformed SQL with many-to-many fields)




Change Properties
Action