do I need to run syncdb when I change the default value in a field of a model?

82 views
Skip to first unread message

Craig Schmidt

unread,
Mar 14, 2014, 9:24:19 PM3/14/14
to django...@googlegroups.com
This is probably an easy question, but I didn’t manage to find the answer.

If I just change the default value of a field in a model, do I need to run syncdb?

Specifically I changed:

foo = models.IntegerField(default=1)

to

foo = models.IntegerField(default=5)

Thanks,
Craig

Shawn Milochik

unread,
Mar 14, 2014, 11:16:20 PM3/14/14
to django...@googlegroups.com
No, no syncdb required. The default applies to newly-created instances, not existing ones.

Jonathan Baker

unread,
Mar 14, 2014, 11:19:29 PM3/14/14
to django...@googlegroups.com
Does that mean that the default="" functionality is implemented by the ORM, instead of in the database layer?


On Fri, Mar 14, 2014 at 5:16 PM, Shawn Milochik <shawn...@gmail.com> wrote:
No, no syncdb required. The default applies to newly-created instances, not existing ones.

--
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.
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/CAOzwKwH-5UNR7ajWb%2B-w%3DepQRSUooEk%3DA-gwAjAhE8vtUT%3DA_Q%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Jonathan D. Baker
Developer
http://jonathandbaker.com

Shawn Milochik

unread,
Mar 14, 2014, 11:24:25 PM3/14/14
to django...@googlegroups.com
On Fri, Mar 14, 2014 at 7:19 PM, Jonathan Baker <jonathand...@gmail.com> wrote:
Does that mean that the default="" functionality is implemented by the ORM, instead of in the database layer?


Perhaps I'm wrong -- I'm looking at my South migrations and they do pass the defaults. I believe that, either way, if you only touch the database through the ORM it'll be set properly. It probably depends on what database you're using.

However, there's a bigger issue -- if you run syncdb a second time, it will do nothing, so it doesn't matter either way. If you want to modify an existing table you need to use South (or do it manually). Syncdb will not touch existing tables -- it only adds new ones. Or you can wipe out your whole database and then run syncdb, that would fix it if you don't care about your data.

Shawn 

Craig Schmidt

unread,
Mar 15, 2014, 12:28:00 AM3/15/14
to django...@googlegroups.com
In my case, it is for a system that isn’t in production yet, so I was planning to wipe the database before running syncdb.  I just didn’t know if that is necessary.

-Craig



Shawn 

--
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.
Visit this group at http://groups.google.com/group/django-users.

Russell Keith-Magee

unread,
Mar 15, 2014, 1:41:23 AM3/15/14
to Django Users
On Sat, Mar 15, 2014 at 7:19 AM, Jonathan Baker <jonathand...@gmail.com> wrote:
Does that mean that the default="" functionality is implemented by the ORM, instead of in the database layer?

This is correct - Django's "default" argument is an API level default, not a database level default.

Yours,
Russ Magee %-) 
 

Venkatraman S

unread,
Mar 20, 2014, 10:50:55 AM3/20/14
to django...@googlegroups.com
@Russ, any specific reasons for this choice?

Regards,
Venkat 

Russell Keith-Magee

unread,
Mar 21, 2014, 12:13:11 AM3/21/14
to Django Users
Honestly - not a clue. It was like that when I got here, so the reasons are probably lost in the depths of time. :-)

Trying to reverse engineer the decision, the most likely reason is that Django has allowed callable defaults; if you define a callable in your Django model as a default, there's no way to pass this down to the database (or, at least, not an easy way). 

Yours,
Russ Magee %-) 
 

Mike Dewhirst

unread,
Mar 21, 2014, 12:35:02 AM3/21/14
to django...@googlegroups.com
On 21/03/2014 11:13am, Russell Keith-Magee wrote:
>
> On Thu, Mar 20, 2014 at 6:50 PM, Venkatraman S <venk...@gmail.com
> <mailto:venk...@gmail.com>> wrote:
>
>
> On Sat, Mar 15, 2014 at 7:11 AM, Russell Keith-Magee
> <rus...@keith-magee.com <mailto:rus...@keith-magee.com>> wrote:
>
>
> On Sat, Mar 15, 2014 at 7:19 AM, Jonathan Baker
> <jonathand...@gmail.com
> <mailto:jonathand...@gmail.com>> wrote:
>
> Does that mean that the default="" functionality is
> implemented by the ORM, instead of in the database layer?
>
> This is correct - Django's "default" argument is an API level
> default, not a database level default.
>
>
> @Russ, any specific reasons for this choice?
>
>
> Honestly - not a clue. It was like that when I got here, so the reasons
> are probably lost in the depths of time. :-)

If some things are done in the database and some in the ORM, developer
madness ensues.

I think keeping everything in the ORM is entirely manageable for
programmers. Tweaking the database is last straw optimisation.

Mike

>
> Trying to reverse engineer the decision, the most likely reason is that
> Django has allowed callable defaults; if you define a callable in your
> Django model as a default, there's no way to pass this down to the
> database (or, at least, not an easy way).
>
> Yours,
> Russ Magee %-)
>
> --
> 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/CAJxq848nGKNY%2B4tpfPK80sOT1gwPocvR%2BPbrs6jeQiUcOv1mew%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAJxq848nGKNY%2B4tpfPK80sOT1gwPocvR%2BPbrs6jeQiUcOv1mew%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Venkatraman S

unread,
Apr 7, 2014, 6:03:49 AM4/7/14
to django...@googlegroups.com
This helped me in one of the cases just now and it was an Aha moment!  Never realized that this feature would be so darn useful. Thanks devs.

-V
@venkasub
 
Reply all
Reply to author
Forward
0 new messages