#23630: AlterModelTable doesn't rename auto-created tables
----------------------------+------------------------------------
Reporter: Naddiseo | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by bmispelon):
* needs_docs: => 0
* needs_better_patch: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted
Comment:
Hi,
Indeed, I can reproduce this issue like so:
Create an app with the following models:
{{{
#!python
class Foo(models.Model):
pass
class Bar(models.Model):
foos = models.ManyToManyField(Foo)
}}}
Run `makemigrations` to create the initial migration then change the `Foo`
model to:
{{{#!python
class Foo(models.Model):
class Meta:
db_table = 'asdf'
}}}
Since the `db_table` change isn't auto-detected (#23629), also create a
migration (`makemigrations --empty appname`):
{{{#!python
class Migration(migrations.Migration):
dependencies = [
('bug23630', '0001_initial'),
]
operations = [
migrations.AlterModelTable(
name='Foo',
table='asdf',
)
]
}}}
After that, run `migrate` to sync the database.
Once that's done, running `Foo.objects.create()` triggers the following
error:
{{{
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "./django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "./django/db/models/query.py", line 372, in create
obj.save(force_insert=True, using=self.db)
File "./django/db/models/base.py", line 591, in save
force_update=force_update, update_fields=update_fields)
File "./django/db/models/base.py", line 619, in save_base
updated = self._save_table(raw, cls, force_insert, force_update,
using, update_fields)
File "./django/db/models/base.py", line 700, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk,
raw)
File "./django/db/models/base.py", line 733, in _do_insert
using=using, raw=raw)
File "./django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "./django/db/models/query.py", line 921, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "./django/db/models/sql/compiler.py", line 920, in execute_sql
cursor.execute(sql, params)
File "./django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "./django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "./django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "./django/utils/six.py", line 549, in reraise
raise value.with_traceback(tb)
File "./django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "./django/db/backends/sqlite3/base.py", line 485, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: asdf
}}}
Ideally, the `AlterModelTable` operation should be smart enough rename the
M2M and foreignkeys pointing to the model whose table has changed.
If that's not possible, the limitation should definitely be documented.
Thanks.
--
Ticket URL: <https://code.djangoproject.com/ticket/23630#comment:1>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.0ba1c8dc9e35b12640deacc184a4a999%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.