mirror of https://github.com/django/django.git
Support index_together during model creation
This commit is contained in:
parent
21be9fef7b
commit
ae19315b4d
|
@ -210,6 +210,15 @@ class BaseDatabaseSchemaEditor(object):
|
|||
"definition": ", ".join(column_sqls)
|
||||
}
|
||||
self.execute(sql, params)
|
||||
# Add any index_togethers
|
||||
for fields in model._meta.index_together:
|
||||
columns = [model._meta.get_field_by_name(field)[0].column for field in fields]
|
||||
self.execute(self.sql_create_index % {
|
||||
"table": self.quote_name(model._meta.db_table),
|
||||
"name": self._create_index_name(model, columns, suffix="_idx"),
|
||||
"columns": ", ".join(self.quote_name(column) for column in columns),
|
||||
"extra": "",
|
||||
})
|
||||
# Make M2M tables
|
||||
for field in model._meta.local_many_to_many:
|
||||
self.create_model(field.rel.through)
|
||||
|
|
|
@ -168,7 +168,10 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
|
|||
"""
|
||||
# Don't use PRAGMA because that causes issues with some transactions
|
||||
cursor.execute("SELECT sql FROM sqlite_master WHERE tbl_name = %s AND type = %s", [table_name, "table"])
|
||||
results = cursor.fetchone()[0].strip()
|
||||
row = cursor.fetchone()
|
||||
if row is None:
|
||||
raise ValueError("Table %s does not exist" % table_name)
|
||||
results = row[0].strip()
|
||||
results = results[results.index('(') + 1:results.rindex(')')]
|
||||
for field_desc in results.split(','):
|
||||
field_desc = field_desc.strip()
|
||||
|
|
|
@ -62,6 +62,15 @@ class Tag(models.Model):
|
|||
app_cache = new_app_cache
|
||||
|
||||
|
||||
class TagIndexed(models.Model):
|
||||
title = models.CharField(max_length=255)
|
||||
slug = models.SlugField(unique=True)
|
||||
|
||||
class Meta:
|
||||
app_cache = new_app_cache
|
||||
index_together = [["slug", "title"]]
|
||||
|
||||
|
||||
class TagUniqueRename(models.Model):
|
||||
title = models.CharField(max_length=255)
|
||||
slug2 = models.SlugField(unique=True)
|
||||
|
|
|
@ -6,7 +6,7 @@ from django.db import connection, DatabaseError, IntegrityError
|
|||
from django.db.models.fields import IntegerField, TextField, CharField, SlugField
|
||||
from django.db.models.fields.related import ManyToManyField, ForeignKey
|
||||
from django.db.transaction import atomic
|
||||
from .models import Author, AuthorWithM2M, Book, BookWithSlug, BookWithM2M, Tag, TagUniqueRename, UniqueTest
|
||||
from .models import Author, AuthorWithM2M, Book, BookWithSlug, BookWithM2M, Tag, TagIndexed, TagUniqueRename, UniqueTest
|
||||
|
||||
|
||||
class SchemaTests(TransactionTestCase):
|
||||
|
@ -503,6 +503,23 @@ class SchemaTests(TransactionTestCase):
|
|||
),
|
||||
)
|
||||
|
||||
def test_create_index_together(self):
|
||||
"""
|
||||
Tests creating models with index_together already defined
|
||||
"""
|
||||
# Create the table
|
||||
with connection.schema_editor() as editor:
|
||||
editor.create_model(TagIndexed)
|
||||
# Ensure there is an index
|
||||
self.assertEqual(
|
||||
True,
|
||||
any(
|
||||
c["index"]
|
||||
for c in connection.introspection.get_constraints(connection.cursor(), "schema_tagindexed").values()
|
||||
if c['columns'] == ["slug", "title"]
|
||||
),
|
||||
)
|
||||
|
||||
def test_db_table(self):
|
||||
"""
|
||||
Tests renaming of the table
|
||||
|
|
Loading…
Reference in New Issue