Ticket #11107: many_to_many.rel.through.4.diff

File many_to_many.rel.through.4.diff, 4.8 KB (added by Cliff Dyer, 15 years ago)

Patch now includes test. Test passes against postgresql and postgresql_psycopg2, needs to be run against oracle.

  • django/db/backends/postgresql/operations.py

     
    121121                        style.SQL_TABLE(qn(model._meta.db_table))))
    122122                    break # Only one AutoField is allowed per model, so don't bother continuing.
    123123            for f in model._meta.many_to_many:
    124                 output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
    125                     (style.SQL_KEYWORD('SELECT'),
    126                     style.SQL_FIELD(qn('%s_id_seq' % f.m2m_db_table())),
    127                     style.SQL_FIELD(qn('id')),
    128                     style.SQL_FIELD(qn('id')),
    129                     style.SQL_KEYWORD('IS NOT'),
    130                     style.SQL_KEYWORD('FROM'),
    131                     style.SQL_TABLE(qn(f.m2m_db_table()))))
     124                if not f.rel.through:
     125                    output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
     126                        (style.SQL_KEYWORD('SELECT'),
     127                        style.SQL_FIELD(qn('%s_id_seq' % f.m2m_db_table())),
     128                        style.SQL_FIELD(qn('id')),
     129                        style.SQL_FIELD(qn('id')),
     130                        style.SQL_KEYWORD('IS NOT'),
     131                        style.SQL_KEYWORD('FROM'),
     132                        style.SQL_TABLE(qn(f.m2m_db_table()))))
    132133        return output
    133134
    134135    def savepoint_create_sql(self, sid):
  • django/db/backends/oracle/base.py

     
    217217                    # continue to loop
    218218                    break
    219219            for f in model._meta.many_to_many:
    220                 table_name = self.quote_name(f.m2m_db_table())
    221                 sequence_name = get_sequence_name(f.m2m_db_table())
    222                 column_name = self.quote_name('id')
    223                 output.append(query % {'sequence': sequence_name,
    224                                        'table': table_name,
    225                                        'column': column_name})
     220                if not f.rel.through:
     221                    table_name = self.quote_name(f.m2m_db_table())
     222                    sequence_name = get_sequence_name(f.m2m_db_table())
     223                    column_name = self.quote_name('id')
     224                    output.append(query % {'sequence': sequence_name,
     225                                           'table': table_name,
     226                                           'column': column_name})
    226227        return output
    227228
    228229    def start_transaction_sql(self):
  • tests/regressiontests/m2m_through_regress/models.py

     
    1212    def __unicode__(self):
    1313        return "%s is a member of %s" % (self.person.name, self.group.name)
    1414
     15# using custom id column to test ticket #11107
    1516class UserMembership(models.Model):
     17    id = models.AutoField(db_column='usermembership_id', primary_key=True)
    1618    user = models.ForeignKey(User)
    1719    group = models.ForeignKey('Group')
    1820    price = models.IntegerField(default=100)
     
    196198# Flush the database, just to make sure we can.
    197199>>> management.call_command('flush', verbosity=0, interactive=False)
    198200
     201## Regression test for #11107
     202Ensure that sequences on m2m_through tables are being created for the through
     203model, not for a phantom auto-generated m2m table.
     204
     205>>> management.call_command('sqlsequencereset', 'm2m_through_regress')
     206BEGIN;
     207SELECT setval('"m2m_through_regress_membership_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "m2m_through_regress_membership";
     208SELECT setval('"m2m_through_regress_usermembership_usermembership_id_seq"', coalesce(max("usermembership_id"), 1), max("usermembership_id") IS NOT null) FROM "m2m_through_regress_usermembership";
     209SELECT setval('"m2m_through_regress_person_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "m2m_through_regress_person";
     210SELECT setval('"m2m_through_regress_group_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "m2m_through_regress_group";
     211SELECT setval('"m2m_through_regress_a_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "m2m_through_regress_a";
     212SELECT setval('"m2m_through_regress_throughbase_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "m2m_through_regress_throughbase";
     213SELECT setval('"m2m_through_regress_b_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "m2m_through_regress_b";
     214COMMIT;
    199215"""}
Back to Top