﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28846	autogenerated table for ManyToMany relation does not include UNIQUE constraint if any other field is changed	Yevhen Kozlov	nobody	"0. create a models.py, ''makemigrations'' for them

{{{
from django.db import models

class Test(models.Model):
	pass

class Test2(models.Model):
	title = models.CharField(max_length=50)
}}}

1. add manyToMany field only, ''makemigrations'':

{{{
from django.db import models

class Test(models.Model):
	pass

class Test2(models.Model):
	title = models.CharField(max_length=50)
	tests = models.ManyToManyField(Test)
}}}
 
Check with ''sqlmigrate'' there is table ..._test2_test with UNIQUE INDEX:

{{{
CREATE TABLE ""m2m_test2_tests"" (""id"" integer NOT NULL PRIMARY KEY AUTOINCREMENT, ""test2_id"" integer NOT NULL REFERENCES ""m2m_test2"" (""id""), ""test_id"" integer NOT NULL REFERENCES ""m2m_test"" (""id""));
CREATE UNIQUE INDEX ""m2m_test2_tests_test2_id_test_id_c4708311_uniq"" ON ""m2m_test2_tests"" (""test2_id"", ""test_id"");
CREATE INDEX ""m2m_test2_tests_test2_id_de2c1678"" ON ""m2m_test2_tests"" (""test2_id"");
CREATE INDEX ""m2m_test2_tests_test_id_9723a1f8"" ON ""m2m_test2_tests"" (""test_id"");
}}}

2. drop migration created at step 1 and change title field additionally(say, set max_length=100)

{{{
from django.db import models

class Test(models.Model):
	pass

class Test2(models.Model):
	title = models.CharField(max_length=50)
	tests = models.ManyToManyField(Test)
}}}

Then run ''makemigrations'' and check its output:

{{{
BEGIN;
--
-- Add field tests to test2
--
CREATE TABLE ""m2m_test2_tests"" (""id"" integer NOT NULL PRIMARY KEY AUTOINCREMENT, ""test2_id"" integer NOT NULL REFERENCES ""m2m_test2"" (""id""), ""test_id"" integer NOT NULL REFERENCES ""m2m_test"" (""id""));
--
-- Alter field title on test2
--
ALTER TABLE ""m2m_test2"" RENAME TO ""m2m_test2__old"";
CREATE TABLE ""m2m_test2"" (""id"" integer NOT NULL PRIMARY KEY AUTOINCREMENT, ""title"" varchar(50) NOT NULL);
INSERT INTO ""m2m_test2"" (""id"", ""title"") SELECT ""id"", ""title"" FROM ""m2m_test2__old"";
DROP TABLE ""m2m_test2__old"";
COMMIT;
}}}

So no UNIQUE constraint is generated for automatically created table if there is any other field changed in the same migration."	Bug	new	Migrations	1.11	Normal				Unreviewed	0	0	0	0	0	0
