Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django sync db question

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 fields 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)
like image 590
Hulk Avatar asked Nov 25 '25 21:11

Hulk


1 Answers

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()

like image 187
Amarghosh Avatar answered Nov 28 '25 10:11

Amarghosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!