gpt4 book ai didi

python - django 同步数据库问题

转载 作者:行者123 更新时间:2023-11-28 19:59:51 27 4
gpt4 key购买 nike

在django模型中说这个模型存在于details/models.py

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

因此,当创建同步数据库时,将创建以下字段

后来又添加了两个字段,并使同步数据库不创建新字段。如何解决这个问题,还有下面的 auto_now=true 是什么

这些是新字段

         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.

你可以

  • 发出手动 ALTER TABLE 命令
  • DROP TABLE 特定表(将丢失数据)并再次运行 syncdb
  • 运行django-admin sqlclear获取 sql 语句列表以清除整个数据库并运行这些命令(将刷新数据库 - 您将丢失所有现有数据)或

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.

因此,modified_date 列将在您每次调用 object.save() 时自动更新

关于python - django 同步数据库问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2902800/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com