开发者

django sync db question

开发者 https://www.devze.com 2022-12-31 18:09 出处:网络
In django models say this model exist in details/models.py class OccDetails(models.Model): title = models.CharField(max_length = 255)

In django models say this model exist in details/models.py

    class OccDetails(models.Model):
       title = models.CharField(max_length = 255)
       occ = models.ForeignKey(Occ)

So when sync db is made the following fields get created

and later to this of two more fields are added and sync db is made the new fie开发者_Python百科lds doesnt get created.How is this to be solved,Also what is auto_now=true in the below

these are the new fields

         created_date = models.DateTimeField(auto_now_add=True)
         modified_date = models.DateTimeField(auto_now_add=True, auto_now=True)


syncdb creates the database tables for all apps in INSTALLED_APPS whose tables have not already been created.

Syncdb will not alter existing tables
syncdb will only create tables for models which have not yet been installed. It will never issue ALTER TABLE statements to match changes made to a model class after installation. Changes to model classes and database schemas often involve some form of ambiguity and, in those cases, Django would have to guess at the correct changes to make. There is a risk that critical data would be lost in the process.

you can either

  • Issue a manual ALTER TABLE command
  • DROP TABLE the particular table (will lose data) and run syncdb again
  • run django-admin sqlclear to get a list of sql statements to clear the entire db and run those commands (will flush the db - you'll lose all existing data) or

DateField.auto_now: automatically set the field to NOW() every time the object is saved. Useful for "last-modified" timestamps. Note that the current date is always used; it's not just a default value that you can override.

Thus, the modified_date column will be automatically updated every time you call object.save()


This is a common problem with Django. As said by Amarghosh, syncdb can not modify the schema of existing tables.

South has been created to solve this problem.

I do recommend it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号