Ticket #24954: test_many2many_intermediate_table_not_migrated.diff

File test_many2many_intermediate_table_not_migrated.diff, 4.0 KB (added by Alexander, 9 years ago)

Many to many intermediate table is not migrated.

  • tests/migrations/test_operations.py

    diff --git tests/migrations/test_operations.py tests/migrations/test_operations.py
    index c8f1189..b252424 100644
    class OperationTests(OperationTestBase):  
    18501850        self.assertEqual(definition[1], [])
    18511851        self.assertEqual(sorted(definition[2]), ["database_operations", "state_operations"])
    18521852
     1853    def test_m2m_intermediate_table_is_not_migrated_properly(self):
     1854        """
     1855        Primary key changes are not reflected in m2m intermediate table
     1856        """
     1857        def initial_data(models, schema_editor):
     1858            Article = models.get_model("test_m2m", "Article")
     1859            Tag = models.get_model("test_m2m", "Tag")
     1860            tag1 = Tag.objects.create(name='framework')
     1861            tag2 = Tag.objects.create(name='web')
     1862            article = Article.objects.create(name="Frameworks", data="Hello World", id='a' * 5)
     1863            article.tag.add(tag1, tag2)
     1864
     1865        def big_data(models, schema_editor):
     1866            Article = models.get_model("test_m2m", "Article")
     1867            Tag = models.get_model("test_m2m", "Tag")
     1868            tag1 = Tag.objects.create(name='data')
     1869            article = Article.objects.create(name="Frameworks", data="Hello World", id='b' * 20)
     1870            article.tag.add(tag1)
     1871
     1872        create_tag = migrations.CreateModel(
     1873            "Tag",
     1874            [
     1875                ("name", models.CharField(max_length=50)),
     1876            ],
     1877            options={},
     1878        )
     1879
     1880        create_article = migrations.CreateModel(
     1881            "Article",
     1882            [
     1883                ("id", models.CharField(primary_key=True, max_length=10)),
     1884                ("name", models.CharField(max_length=100)),
     1885                ("data", models.TextField(default="")),
     1886                ("tag", models.ManyToManyField(to="test_m2m.Tag")),
     1887            ],
     1888            options={},
     1889        )
     1890
     1891        fill_initial_data = migrations.RunPython(initial_data, initial_data)
     1892
     1893        def grow_article_id(new_state):
     1894            _grow_article_id = migrations.AlterField(
     1895                "Article",
     1896                "id",
     1897                models.CharField(primary_key=True, max_length=50)
     1898            )
     1899            project_state = new_state
     1900            new_state = new_state.clone()
     1901            with connection.schema_editor() as editor:
     1902                _grow_article_id.state_forwards("test_m2m", new_state)
     1903                _grow_article_id.database_forwards("test_m2m", editor, project_state, new_state)
     1904            return new_state
     1905
     1906        def fill_big_data(new_state):
     1907            project_state = new_state
     1908            new_state = new_state.clone()
     1909            _fill_big_data = migrations.RunPython(big_data, big_data)
     1910            with connection.schema_editor() as editor:
     1911                _fill_big_data.state_forwards("fill_big_data", new_state)
     1912                _fill_big_data.database_forwards("fill_big_data", editor, project_state, new_state)
     1913            return new_state
     1914
     1915        project_state = ProjectState()
     1916        new_state = project_state.clone()
     1917        with connection.schema_editor() as editor:
     1918            create_tag.state_forwards("test_m2m", new_state)
     1919            create_tag.database_forwards("test_m2m", editor, project_state, new_state)
     1920
     1921        project_state = new_state
     1922        new_state = new_state.clone()
     1923        with connection.schema_editor() as editor:
     1924            create_article.state_forwards("test_m2m", new_state)
     1925            create_article.database_forwards("test_m2m", editor, project_state, new_state)
     1926
     1927        project_state = new_state
     1928        new_state = new_state.clone()
     1929        with connection.schema_editor() as editor:
     1930            fill_initial_data.state_forwards("fill_initial_data", new_state)
     1931            fill_initial_data.database_forwards("fill_initial_data", editor, project_state, new_state)
     1932
     1933        new_state = grow_article_id(new_state)
     1934
     1935        fill_big_data(new_state)  # no exception for sqlite, IntegrityError or DataError for other backends
     1936
    18531937
    18541938class SwappableOperationTests(OperationTestBase):
    18551939    """
Back to Top