Code

Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#584 closed defect (duplicate)

[patch] fix many-to-many with non-default pks.

Reported by: rjwittams Owned by: adrian
Component: Metasystem Version:
Severity: major Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I found that many to many fields do not work when the primary keys of the objects are not default.
As I'm working in the new-admin branch and this is an obvious fix, I thought I'd point it up here so
it can be applied to trunk.

def method_set_related_many_to_many(rel_opts, rel_field, self, id_list):
    id_list = map(int, id_list) # normalize to integers
    rel = rel_field.rel.to
    m2m_table = rel_field.get_m2m_db_table(rel_opts)
    this_id = getattr(self, self._meta.pk.column)
    cursor = db.db.cursor()
    delete_stmt = "DELETE FROM %s WHERE %s = %%s" % (m2m_table, rel.pk.column)
    cursor.execute(delete_stmt, [this_id])
    insert_stmt = "INSERT INTO %s (%s, %s) VALUES (%%s, %%s)" % (m2m_table, rel.pk.column, rel_opts.pk.column)
    cursor.executemany(insert_stmt, [(this_id, i) for i in id_list])
    db.db.commit()

Attachments (0)

Change History (7)

comment:1 Changed 9 years ago by adrian

  • Resolution set to duplicate
  • Status changed from new to closed

This is a duplicate of #556 and was fixed in [682]. Maybe [682] didn't get applied to your new-admin branch?

comment:2 Changed 9 years ago by rjwittams

  • Resolution duplicate deleted
  • Status changed from closed to reopened

Sorry, that fix won't work.

Look at the original version of this function.

comment:4 Changed 9 years ago by adrian

Could you clarify, please?

comment:5 Changed 9 years ago by rjwittams

  • Resolution set to duplicate
  • Status changed from reopened to closed

Hm, well 684 is before my branch started.
I can see how that is supposed to work to work now.
I don't get how my fix worked with this change in place as well.....

comment:6 Changed 9 years ago by adrian

I just realized the fix in [682] was to method_set_many_to_many(), whereas you're pointing out method_set_related_many_to_many().

comment:7 Changed 7 years ago by anonymous

  • milestone Version 1.0 deleted

Milestone Version 1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.