mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Fixed #14286 -- Added models.BigAutoField.
This commit is contained in:
committed by
Tim Graham
parent
a1d0c60fa0
commit
2a7ce34600
@@ -1806,6 +1806,144 @@ class OperationTests(OperationTestBase):
|
||||
create_old_man.state_forwards("test_books", new_state)
|
||||
create_old_man.database_forwards("test_books", editor, project_state, new_state)
|
||||
|
||||
def test_model_with_bigautofield(self):
|
||||
"""
|
||||
A model with BigAutoField can be created.
|
||||
"""
|
||||
def create_data(models, schema_editor):
|
||||
Author = models.get_model("test_author", "Author")
|
||||
Book = models.get_model("test_book", "Book")
|
||||
author1 = Author.objects.create(name="Hemingway")
|
||||
Book.objects.create(title="Old Man and The Sea", author=author1)
|
||||
Book.objects.create(id=2 ** 33, title="A farewell to arms", author=author1)
|
||||
|
||||
author2 = Author.objects.create(id=2 ** 33, name="Remarque")
|
||||
Book.objects.create(title="All quiet on the western front", author=author2)
|
||||
Book.objects.create(title="Arc de Triomphe", author=author2)
|
||||
|
||||
create_author = migrations.CreateModel(
|
||||
"Author",
|
||||
[
|
||||
("id", models.BigAutoField(primary_key=True)),
|
||||
("name", models.CharField(max_length=100)),
|
||||
],
|
||||
options={},
|
||||
)
|
||||
create_book = migrations.CreateModel(
|
||||
"Book",
|
||||
[
|
||||
("id", models.BigAutoField(primary_key=True)),
|
||||
("title", models.CharField(max_length=100)),
|
||||
("author", models.ForeignKey(to="test_author.Author", on_delete=models.CASCADE))
|
||||
],
|
||||
options={},
|
||||
)
|
||||
fill_data = migrations.RunPython(create_data)
|
||||
|
||||
project_state = ProjectState()
|
||||
new_state = project_state.clone()
|
||||
with connection.schema_editor() as editor:
|
||||
create_author.state_forwards("test_author", new_state)
|
||||
create_author.database_forwards("test_author", editor, project_state, new_state)
|
||||
|
||||
project_state = new_state
|
||||
new_state = new_state.clone()
|
||||
with connection.schema_editor() as editor:
|
||||
create_book.state_forwards("test_book", new_state)
|
||||
create_book.database_forwards("test_book", editor, project_state, new_state)
|
||||
|
||||
project_state = new_state
|
||||
new_state = new_state.clone()
|
||||
with connection.schema_editor() as editor:
|
||||
fill_data.state_forwards("fill_data", new_state)
|
||||
fill_data.database_forwards("fill_data", editor, project_state, new_state)
|
||||
|
||||
def test_autofield_foreignfield_growth(self):
|
||||
"""
|
||||
A field may be migrated from AutoField to BigAutoField.
|
||||
"""
|
||||
def create_initial_data(models, schema_editor):
|
||||
Article = models.get_model("test_article", "Article")
|
||||
Blog = models.get_model("test_blog", "Blog")
|
||||
blog = Blog.objects.create(name="web development done right")
|
||||
Article.objects.create(name="Frameworks", blog=blog)
|
||||
Article.objects.create(name="Programming Languages", blog=blog)
|
||||
|
||||
def create_big_data(models, schema_editor):
|
||||
Article = models.get_model("test_article", "Article")
|
||||
Blog = models.get_model("test_blog", "Blog")
|
||||
blog2 = Blog.objects.create(name="Frameworks", id=2 ** 33)
|
||||
Article.objects.create(name="Django", blog=blog2)
|
||||
Article.objects.create(id=2 ** 33, name="Django2", blog=blog2)
|
||||
|
||||
create_blog = migrations.CreateModel(
|
||||
"Blog",
|
||||
[
|
||||
("id", models.AutoField(primary_key=True)),
|
||||
("name", models.CharField(max_length=100)),
|
||||
],
|
||||
options={},
|
||||
)
|
||||
create_article = migrations.CreateModel(
|
||||
"Article",
|
||||
[
|
||||
("id", models.AutoField(primary_key=True)),
|
||||
("blog", models.ForeignKey(to="test_blog.Blog", on_delete=models.CASCADE)),
|
||||
("name", models.CharField(max_length=100)),
|
||||
("data", models.TextField(default="")),
|
||||
],
|
||||
options={},
|
||||
)
|
||||
fill_initial_data = migrations.RunPython(create_initial_data, create_initial_data)
|
||||
fill_big_data = migrations.RunPython(create_big_data, create_big_data)
|
||||
|
||||
grow_article_id = migrations.AlterField("Article", "id", models.BigAutoField(primary_key=True))
|
||||
grow_blog_id = migrations.AlterField("Blog", "id", models.BigAutoField(primary_key=True))
|
||||
|
||||
project_state = ProjectState()
|
||||
new_state = project_state.clone()
|
||||
with connection.schema_editor() as editor:
|
||||
create_blog.state_forwards("test_blog", new_state)
|
||||
create_blog.database_forwards("test_blog", editor, project_state, new_state)
|
||||
|
||||
project_state = new_state
|
||||
new_state = new_state.clone()
|
||||
with connection.schema_editor() as editor:
|
||||
create_article.state_forwards("test_article", new_state)
|
||||
create_article.database_forwards("test_article", editor, project_state, new_state)
|
||||
|
||||
project_state = new_state
|
||||
new_state = new_state.clone()
|
||||
with connection.schema_editor() as editor:
|
||||
fill_initial_data.state_forwards("fill_initial_data", new_state)
|
||||
fill_initial_data.database_forwards("fill_initial_data", editor, project_state, new_state)
|
||||
|
||||
project_state = new_state
|
||||
new_state = new_state.clone()
|
||||
with connection.schema_editor() as editor:
|
||||
grow_article_id.state_forwards("test_article", new_state)
|
||||
grow_article_id.database_forwards("test_article", editor, project_state, new_state)
|
||||
|
||||
state = new_state.clone()
|
||||
article = state.apps.get_model("test_article.Article")
|
||||
self.assertIsInstance(article._meta.pk, models.BigAutoField)
|
||||
|
||||
project_state = new_state
|
||||
new_state = new_state.clone()
|
||||
with connection.schema_editor() as editor:
|
||||
grow_blog_id.state_forwards("test_blog", new_state)
|
||||
grow_blog_id.database_forwards("test_blog", editor, project_state, new_state)
|
||||
|
||||
state = new_state.clone()
|
||||
blog = state.apps.get_model("test_blog.Blog")
|
||||
self.assertTrue(isinstance(blog._meta.pk, models.BigAutoField))
|
||||
|
||||
project_state = new_state
|
||||
new_state = new_state.clone()
|
||||
with connection.schema_editor() as editor:
|
||||
fill_big_data.state_forwards("fill_big_data", new_state)
|
||||
fill_big_data.database_forwards("fill_big_data", editor, project_state, new_state)
|
||||
|
||||
def test_run_python_noop(self):
|
||||
"""
|
||||
#24098 - Tests no-op RunPython operations.
|
||||
|
||||
Reference in New Issue
Block a user