Opened 20 months ago

Closed 20 months ago

Last modified 20 months ago

#27750 closed Bug (duplicate)

TypeError when running manage.py migrate

Reported by: Henrique Chehad Owned by: nobody
Component: Migrations Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello,

Is returning this error when I try to run ./manage.py migrate in a model:

Model:

class Cast(models.Model):
    movie = models.ForeignKey("movies.Movie")
    artist = models.ForeignKey("artists.Artist", db_column="actor_id")

Migration:

        migrations.AlterField(
            model_name='cast',
            name='artist',
            field=models.ForeignKey(db_column='actor_id', on_delete=django.db.models.deletion.CASCADE, to='artists.Artist'),
        ),

Error:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/site-packages/raven/contrib/django/management/__init__.py", line 41, in new_execute
    return original_func(self, *args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.5/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.5/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.5/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.5/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 482, in alter_field
    old_db_params, new_db_params, strict)
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/postgresql/schema.py", line 110, in _alter_field
    new_db_params, strict,
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 545, in _alter_field
    self.execute(self._rename_field_sql(model._meta.db_table, old_field, new_field, new_type))
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 867, in _rename_field_sql
    "old_column": self.quote_name(old_field.column),
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 113, in quote_name
    return self.connection.ops.quote_name(name)
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/postgresql/operations.py", line 95, in quote_name
    if name.startswith('"') and name.endswith('"'):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

The error is in the field artist of Cast model, because I added a print() to discover what is the field with the error when call the function quote_name and printed actor_id. I commented this field in migration just to test and worked.

Detail: I didn't changed anything in this fields, it's because when I migrated from Python 2.7.5 to Python 3.5 the verbose_name changed (removed the u"") and asked to generate new migrations. I think it is unnecessary.

Change History (3)

comment:1 Changed 20 months ago by Tim Graham

Resolution: needsinfo
Status: newclosed
Type: UncategorizedBug

Did you follow the advice about supporting Python 2 and 3? It looks like you might have a field in a migration that uses a bytestring (i.e. b'artist'). The issue is likely obsolete with the removal of Django's support for Python 2, but if you can reproduce some problem using only Python 3, please be more specific about the steps and provide a sample project. Thanks!

comment:2 in reply to:  1 Changed 20 months ago by Henrique Chehad

Thanks.

The older migration file (0001_initial) that contains artist field was with bytestring. I edited it and now the migrate worked. It was affecting the current migration file (0005).

-                ('artist', models.ForeignKey(db_column=b'actor_id', on_delete=django.db.models.deletion.CASCADE, to='artists.Artist')),
+                ('artist', models.ForeignKey(db_column='actor_id', on_delete=django.db.models.deletion.CASCADE, to='artists.Artist')),

Would be good a contribution with clearer error message or converting it before the quote_name function?

Replying to Tim Graham:

Did you follow the advice about supporting Python 2 and 3? It looks like you might have a field in a migration that uses a bytestring (i.e. b'artist'). The issue is likely obsolete with the removal of Django's support for Python 2, but if you can reproduce some problem using only Python 3, please be more specific about the steps and provide a sample project. Thanks!

comment:3 Changed 20 months ago by Tim Graham

Resolution: needsinfoduplicate

#24949 was to possibly implement an enhancement but at this point we're not going to spend time on Python 2 enhancements.

Note: See TracTickets for help on using tickets.
Back to Top