Skip to content

Instantly share code, notes, and snippets.

@shulcsm
Created March 19, 2018 08:44
Show Gist options
  • Save shulcsm/081b3b08778444a9af978596df8b5bce to your computer and use it in GitHub Desktop.
Save shulcsm/081b3b08778444a9af978596df8b5bce to your computer and use it in GitHub Desktop.
class IUniqueIndex(models.Index):
suffix = 'iuq'
sql_template = 'CREATE UNIQUE INDEX %(name)s ON %(table)s (%(columns)s);'
def create_sql(self, model, schema_editor, using=''):
assert schema_editor.connection.vendor == 'postgresql'
fields = [model._meta.get_field(field_name) for field_name, _ in self.fields_orders]
quote_name = schema_editor.quote_name
return self.sql_template % ({
'name': quote_name(self.name),
'table': quote_name(model._meta.db_table),
'columns': ", ".join(['UPPER(%s)' % quote_name(field.column) for field in fields])
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment