#34820: Change the properties of ForeignObject object, such as blank, null, and
execute migrate to report an error
-------------------------------------+-------------------------------------
     Reporter:  puc_dong             |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  4.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  needsinfo
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by puc_dong):

 * component:  Migrations => Database layer (models, ORM)
 * needs_tests:  1 => 0


Old description:

> Execute migrate to report an error
>
> If the models.ForeignObject attribute is used, changing null or blank to
> generate a new migration record will result in an error message when
> executing the migrate again
>

>
> {{{
> File "/Users/donghao/test/test_migations/manage.py", line 22, in <module>
>     execute_from_command_line(sys.argv)
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/core/management/__init__.py", line 442, in
> execute_from_command_line
>     utility.execute()
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/core/management/__init__.py", line 436, in execute
>     self.fetch_command(subcommand).run_from_argv(self.argv)
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/core/management/base.py", line 412, in run_from_argv
>     self.execute(*args, **cmd_options)
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/core/management/base.py", line 458, in execute
>     output = self.handle(*args, **options)
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/core/management/base.py", line 106, in wrapper
>     res = handle_func(*args, **kwargs)
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/core/management/commands/migrate.py", line 356, in handle
>     post_migrate_state = executor.migrate(
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/db/migrations/executor.py", line 135, in migrate
>     state = self._migrate_all_forwards(
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/db/migrations/executor.py", line 167, in
> _migrate_all_forwards
>     state = self.apply_migration(
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/db/migrations/executor.py", line 252, in apply_migration
>     state = migration.apply(state, schema_editor)
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/db/migrations/migration.py", line 132, in apply
>     operation.database_forwards(
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/db/migrations/operations/fields.py", line 235, in
> database_forwards
>     schema_editor.alter_field(from_model, from_field, to_field)
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/db/backends/base/schema.py", line 785, in alter_field
>     if not self._field_should_be_altered(old_field, new_field):
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/db/backends/base/schema.py", line 1530, in
> _field_should_be_altered
>     return self.quote_name(old_field.column) != self.quote_name(
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/db/backends/base/schema.py", line 204, in quote_name
>     return self.connection.ops.quote_name(name)
>   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
> packages/django/db/backends/mysql/operations.py", line 184, in quote_name
>     if name.startswith("`") and name.endswith("`"):
> AttributeError: 'NoneType' object has no attribute 'startswith'
> }}}
>

> https://github.com/django/django/pull/17235

New description:

 Execute migrate to report an error

 If the models.ForeignObject attribute is used, changing null or blank to
 generate a new migration record will result in an error message when
 executing the migrate again

 {{{
 File "/Users/donghao/test/test_migations/manage.py", line 22, in <module>
     execute_from_command_line(sys.argv)
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/core/management/__init__.py", line 442, in
 execute_from_command_line
     utility.execute()
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/core/management/__init__.py", line 436, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/core/management/base.py", line 412, in run_from_argv
     self.execute(*args, **cmd_options)
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/core/management/base.py", line 458, in execute
     output = self.handle(*args, **options)
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/core/management/base.py", line 106, in wrapper
     res = handle_func(*args, **kwargs)
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/core/management/commands/migrate.py", line 356, in handle
     post_migrate_state = executor.migrate(
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/db/migrations/executor.py", line 135, in migrate
     state = self._migrate_all_forwards(
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/db/migrations/executor.py", line 167, in
 _migrate_all_forwards
     state = self.apply_migration(
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/db/migrations/executor.py", line 252, in apply_migration
     state = migration.apply(state, schema_editor)
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/db/migrations/migration.py", line 132, in apply
     operation.database_forwards(
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/db/migrations/operations/fields.py", line 235, in
 database_forwards
     schema_editor.alter_field(from_model, from_field, to_field)
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/db/backends/base/schema.py", line 785, in alter_field
     if not self._field_should_be_altered(old_field, new_field):
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/db/backends/base/schema.py", line 1530, in
 _field_should_be_altered
     return self.quote_name(old_field.column) != self.quote_name(
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/db/backends/base/schema.py", line 204, in quote_name
     return self.connection.ops.quote_name(name)
   File "/Users/donghao/.virtualenvs/simpleui/lib/python3.9/site-
 packages/django/db/backends/mysql/operations.py", line 184, in quote_name
     if name.startswith("`") and name.endswith("`"):
 AttributeError: 'NoneType' object has no attribute 'startswith'
 }}}

 **models:**

 {{{
 # app1:
 class Model1(models.Model):
     name = models.CharField(max_length=50)

 # app2:
 class Model2(models.Model):
     name = models.CharField(max_length=100, db_index=True)
     clu_id = models.BigIntegerField()
     scenes = models.ForeignObject(Model1,
                                   from_fields=['clu_id'],
                                   to_fields=['id'],
                                   null=True,
                                   related_name='model2_config',
                                   on_delete=models.DO_NOTHING)
 }}}

 Successfully executed makemigrations&migrate for the first time. When I
 add blank=True to the scenes field of Model2, and then execute
 makemigrations and migrate to report an error
 All migration files:
 app1.0001_initial.py
 {{{
 class Migration(migrations.Migration):

     initial = True

     dependencies = [
     ]

     operations = [
         migrations.CreateModel(
             name='Model1',
             fields=[
                 ('id', models.AutoField(auto_created=True,
 primary_key=True, serialize=False, verbose_name='ID')),
                 ('name', models.CharField(max_length=50)),
             ],
         ),
     ]

 }}}
 app2.0001_initial.py

 {{{
 class Migration(migrations.Migration):

     initial = True

     dependencies = [
         ('app_1', '0001_initial'),
     ]

     operations = [
         migrations.CreateModel(
             name='Model2',
             fields=[
                 ('id', models.AutoField(auto_created=True,
 primary_key=True, serialize=False, verbose_name='ID')),
                 ('name', models.CharField(db_index=True, max_length=100)),
                 ('clu_id', models.BigIntegerField()),
                 ('scenes', models.ForeignObject(from_fields=['clu_id'],
 null=True, on_delete=django.db.models.deletion.DO_NOTHING,
 related_name='model2_config', to='app_1.model1', to_fields=['id'])),
             ],
         ),
     ]
 }}}

 app2.0002_alter_model2_scenes.py

 {{{
 class Migration(migrations.Migration):

     dependencies = [
         ('app_1', '0001_initial'),
         ('app_2', '0001_initial'),
     ]

     operations = [
         migrations.AlterField(
             model_name='model2',
             name='scenes',
             field=models.ForeignObject(blank=True,
 from_fields=('clu_id',), null=True,
 on_delete=django.db.models.deletion.DO_NOTHING,
 related_name='model2_config', to='app_1.model1', to_fields=('id',)),
         ),
     ]
 }}}


 FIX: https://github.com/django/django/pull/17235

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34820#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018a737c6f61-81c29990-db1d-401f-a1df-6781c41de937-000000%40eu-central-1.amazonses.com.

Reply via email to