Opened 9 years ago

Closed 9 years ago

#24102 closed Cleanup/optimization (fixed)

SchemaEditor.create_model doesn't create a constraint name for unique_together

Reported by: Damien Nozay Owned by: nobody
Component: Migrations Version: 1.7
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In BaseDatabaseSchemaEditor.create_model there are currently no names for unique_together constraints.

http://stackoverflow.com/a/27836476/1733117

class BaseDatabaseSchemaEditor(object):
    # Overrideable SQL templates
    sql_create_table_unique = "UNIQUE (%(columns)s)"
    sql_create_unique = "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s UNIQUE (%(columns)s)"
    sql_delete_unique = "ALTER TABLE %(table)s DROP CONSTRAINT %(name)s"

create_model has this bit:

        # Add any unique_togethers
        for fields in model._meta.unique_together:
            columns = [model._meta.get_field(field).column for field in fields]
            column_sqls.append(self.sql_create_table_unique % {
                "columns": ", ".join(self.quote_name(column) for column in columns),
            })

whereas adding a new unique_together constraint does this:

    def _create_unique_sql(self, model, columns):
        return self.sql_create_unique % {
            "table": self.quote_name(model._meta.db_table),
            "name": self.quote_name(self._create_index_name(model, columns, suffix="_uniq")),
            "columns": ", ".join(self.quote_name(column) for column in columns),
        }

Change History (7)

comment:1 by Damien Nozay, 9 years ago

In BaseDatabaseSchemaEditor.create_model there are currently no names for unique_together constraints.

possible fix:

class BaseDatabaseSchemaEditor(object):
    # Overrideable SQL templates
    sql_create_table_unique = "UNIQUE %(name)s (%(columns)s)"

create_model should have:

        # Add any unique_togethers
        for fields in model._meta.unique_together:
            columns = [model._meta.get_field(field).column for field in fields]
            column_sqls.append(self.sql_create_table_unique % {
                "name": self.quote_name(self._create_index_name(model, columns, suffix="_uniq")),
                "columns": ", ".join(self.quote_name(column) for column in columns),
            })

comment:2 by Damien Nozay, 9 years ago

haven't tested it myself - but maybe the CI system will:
https://github.com/django/django/pull/3862

Last edited 9 years ago by Damien Nozay (previous) (diff)

comment:3 by Tim Graham, 9 years ago

Component: UncategorizedMigrations
Has patch: set
Needs tests: set
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

comment:4 by Tim Graham, 9 years ago

Summary: BaseDatabaseSchemaEditor.create_model: allow unique_together constraint nameSchemaEditor.create_model doesn't create a constraint name for unique_together

comment:5 by Markus Holtermann, 9 years ago

If we add unique_together constraints in create model, can we add index_together as well, please.

in reply to:  5 comment:6 by Damien Nozay, 9 years ago

Replying to MarkusH:

If we add unique_together constraints in create model, can we add index_together as well, please.

please file a separate ticket. IMHO this is a separate concern / request.

comment:7 by Tim Graham, 9 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top