Drop and replace SQL Table automatically when Model is migrated

40 views
Skip to first unread message

Alexander Whatley

unread,
Dec 6, 2015, 3:22:50 PM12/6/15
to Django users
This question may just be due to my inexperience with Django, but whenever I modify one of my Model elements and change its parameters, I have to close my IDE and manually use the command prompt to drop the SQL Table corresponding to the Model, and then migrate the new model, which is very annoying. Is there a built in feature in Django that allows me to get around having to do this, and if not, is there one in development? Thanks.

Mike Dewhirst

unread,
Dec 6, 2015, 7:49:34 PM12/6/15
to django...@googlegroups.com
On 7/12/2015 7:21 AM, Alexander Whatley wrote:
> This question may just be due to my inexperience with Django, but
> whenever I modify one of my Model elements and change its parameters, I
> have to close my IDE and manually use the command prompt to drop the SQL
> Table corresponding to the Model, and then migrate the new model, which
> is very annoying.

It sounds like this is a feature your IDE should provide. Django has the
necessary functionality but won't do it unbidden.


Is there a built in feature in Django that allows me
> to get around having to do this, and if not, is there one in
> development? Thanks.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/06d9f15a-eaff-43e3-90d6-56a0b03c4e97%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/06d9f15a-eaff-43e3-90d6-56a0b03c4e97%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

James Schneider

unread,
Dec 6, 2015, 8:25:04 PM12/6/15
to django...@googlegroups.com


On Dec 6, 2015 4:48 PM, "Mike Dewhirst" <mi...@dewhirst.com.au> wrote:
>
> On 7/12/2015 7:21 AM, Alexander Whatley wrote:
>>
>> This question may just be due to my inexperience with Django, but
>> whenever I modify one of my Model elements and change its parameters, I
>> have to close my IDE and manually use the command prompt to drop the SQL
>> Table corresponding to the Model, and then migrate the new model, which
>> is very annoying.
>

Are you creating a new migration after each set of changes and then applying that new migration? Why is it that you are expecting a table to drop when all you are doing is modifying an existing table/model? If that's the case, the existing table would be altered, not dropped and recreated with the new attributes (which would cause a loss of data and likely other issues with related data).

If the standard migration process is not working, you should post up the errors that you are getting. Your IDE should have little to do with this process, aside from maybe triggering the migration scripts to run if it supports such an operation.

>
> It sounds like this is a feature your IDE should provide. Django has the necessary functionality but won't do it unbidden.
>
>
>
> Is there a built in feature in Django that allows me
>>
>> to get around having to do this, and if not, is there one in
>> development? Thanks.
>>

If you are expecting (well, desiring) this behavior, you'll likely need to write your own management command to perform this task, which is pretty simple. I wrote one that does something similar to what you are expecting, except that it drops the entire database, creates a new one, applies all existing migrations, and then populates the DB with dummy data that I've generated (since I'm working on models that expect that data to be present already and its a pain to manually create it via the web browser every time). Think of an online demo of an application that resets itself every hour.

https://docs.djangoproject.com/en/1.9/howto/custom-management-commands/

I do think a built-in admin command to "reset" the entire database would be handy, but I've been too lazy to put in the feature request.

-James

Alexander Whatley

unread,
Dec 6, 2015, 8:48:05 PM12/6/15
to Django users
I will check with the people who made the IDE. Thanks for the input.

Jani Tiainen

unread,
Dec 7, 2015, 6:03:33 AM12/7/15
to django...@googlegroups.com
Django has built-in support for database migrations since version 1.7. In normal conditions you should be using at least version 1.8 (LTS, and lowest version supported now).
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages