[Django] #24650: Migration changing db_table and managed not creating new table

17 views
Skip to first unread message

Django

unread,
Apr 16, 2015, 12:10:30 PM4/16/15
to django-...@googlegroups.com
#24650: Migration changing db_table and managed not creating new table
-------------------------------+--------------------
Reporter: duduklein | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
I had a model like this:

{{{
class Decoupe(models.Model):
constante = models.IntegerField()

class Meta:
managed = False
db_table = "Decoupe"
}}}

I ran the first migration and it recognized the options as expected.
Then I removed the meta options, so the model looked like this:
{{{
class Decoupe(models.Model):
constante = models.IntegerField()
}}}

I ran make migrations and it created the following:

{{{
migrations.AlterModelOptions(
name='decoupe',
options={},
),
}}}

When I applied it, nothing really happened and of course, when, trying
from the shell, to to `Decoupe.objects.all()`, I got an error saying that
the table "myapp_decoupe" did not exits.

I would expect either the table to be renamed or at least created empty.

I tried to workaround this replacing the `migrations` above with
{{{
migrations.DeleteModel("decoupe"),
migrations.CreateModel( ... ),
}}}
But when I ran it, I got a error:
`RuntimeError: Error creating new content types. Please make sure
contenttypes is migrated before trying to migrate apps individually.`,
although the contenttypes is migrated.

Is this a bug or desired behavior since the model was "managed=False"?

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

Django

unread,
Apr 16, 2015, 12:30:23 PM4/16/15
to django-...@googlegroups.com
#24650: Migration changing db_table and managed not creating new table
-------------------------------+--------------------------------------

Reporter: duduklein | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by duduklein):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Sorry, I was in django 1.7.

I also tried removing the "managed=False" from the initial migration and
got the same contenttype error. I did not understand why, since I was not
changing any content type.

I upgraded then to 1.8. The automatically generated migration was the same
and applying it did not change a thing, so I think there is still an
issue. Trying to delete and re-create the model as above raised the same
error.

What did work was removing the "managed=False" from the initial migration.
Django then renamed the table as expected.

Is it intended behavior that any model marked as "managed=False" should
never be changed with django's migration?

--
Ticket URL: <https://code.djangoproject.com/ticket/24650#comment:1>

Django

unread,
Apr 16, 2015, 12:51:50 PM4/16/15
to django-...@googlegroups.com
#24650: Migration changing db_table and managed not creating new table
-------------------------------+--------------------------------------

Reporter: duduklein | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by timgraham):

If `managed=False` then you're saying that the table already exists, so I
don't think changing it to `managed=True` should issue a `CREATE TABLE`.
The contenttypes error is probably triggered because the table doesn't
exist. Can you try doing it in two steps: remove `managed=False`, run
`makemigrations`, then remove `db_table` and run `makemigrations`.

--
Ticket URL: <https://code.djangoproject.com/ticket/24650#comment:2>

Django

unread,
Apr 17, 2015, 3:55:15 AM4/17/15
to django-...@googlegroups.com
#24650: Migration changing db_table and managed not creating new table
-------------------------------+--------------------------------------

Reporter: duduklein | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by duduklein):

Thanks for your quick response.
Doing it in 2 steps worked in django 1.8 (I did not test in django 1.7).

Regarding the contenttype issue, the table (and the model) did exist. This
issue does not appear in Django 1.8 anymore, so I'm not sure it's worth
digging further.

Maybe it's worth mentioning in the docs that changing a model from the
`managed=False` state to `managed=True` and making other changes should be
done in 2 steps, as you suggested.

Thanks again.

--
Ticket URL: <https://code.djangoproject.com/ticket/24650#comment:3>

Django

unread,
Apr 17, 2015, 4:39:26 AM4/17/15
to django-...@googlegroups.com
#24650: Migration changing db_table and managed not creating new table
--------------------------------------+------------------------------------
Reporter: duduklein | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.8
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 MarkusH):

* component: Migrations => Documentation
* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/24650#comment:4>

Django

unread,
Aug 18, 2016, 8:05:47 PM8/18/16
to django-...@googlegroups.com
#24650: Migration changing db_table and managed not creating new table
--------------------------------------+------------------------------------
Reporter: duduklein | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/7121 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/24650#comment:5>

Django

unread,
Aug 23, 2016, 5:36:02 PM8/23/16
to django-...@googlegroups.com
#24650: Migration changing db_table and managed not creating new table
--------------------------------------+------------------------------------
Reporter: duduklein | Owner: nobody
Type: Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.8
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by GitHub <noreply@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"80c03b06ad1461c1a07091806dc0414a4e98905e" 80c03b06]:
{{{
#!CommitTicketReference repository=""
revision="80c03b06ad1461c1a07091806dc0414a4e98905e"
Fixed #24650 -- Documented how to change an unmanaged model to managed.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24650#comment:6>

Django

unread,
Aug 23, 2016, 5:36:15 PM8/23/16
to django-...@googlegroups.com
#24650: Migration changing db_table and managed not creating new table
--------------------------------------+------------------------------------
Reporter: duduklein | Owner: nobody

Type: Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.8
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"6be255a22a32a58a96b8d1c67485e92b49817b9f" 6be255a2]:
{{{
#!CommitTicketReference repository=""
revision="6be255a22a32a58a96b8d1c67485e92b49817b9f"
[1.10.x] Fixed #24650 -- Documented how to change an unmanaged model to
managed.

Backport of 80c03b06ad1461c1a07091806dc0414a4e98905e from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24650#comment:7>

Reply all
Reply to author
Forward
0 new messages