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

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

Patch with test that doesn't rely on exact sql syntax of particular backends.

  • 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/fixtures/m2m_through.json

     
     1[
     2    {
     3        "pk": "1",
     4        "model": "m2m_through_regress.person",
     5        "fields": {
     6            "name": "Guido"
     7        }
     8    },
     9    {
     10        "pk": "1",
     11        "model": "auth.user",
     12        "fields": {
     13             "username": "Guido",
     14             "email": "bdfl@python.org",
     15             "password": "abcde"
     16        }
     17    },
     18    {
     19        "pk": "1",
     20        "model": "m2m_through_regress.group",
     21        "fields": {
     22            "name": "Python Core Group"
     23        }
     24    },
     25    {
     26        "pk": "1",
     27        "model": "m2m_through_regress.usermembership",
     28        "fields": {
     29            "user": "1",
     30            "group": "1",
     31            "price": "100"
     32        }
     33    }
     34]
     35 No newline at end of file
  • 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('loaddata', 'm2m_through', verbosity=0)
     206
    199207"""}
  • docs/ref/django-admin.txt

     
    611611
    612612Prints the SQL statements for resetting sequences for the given app name(s).
    613613
    614 See http://simon.incutio.com/archive/2004/04/21/postgres for more information.
     614See http://simonwillison.net/2004/Apr/21/postgres/ for more information.
    615615
    616616startapp <appname>
    617617------------------
Back to Top