有没有办法根据模型中的更改来更新数据库?

发布于 2024-11-27 11:27:48 字数 424 浏览 6 评论 0原文

可能的重复:
更新 django 数据库以反映现有模型中的更改 < /p>

我过去使用过 Django,将其作为 ORM 工具时遇到的挫折之一是无法通过模型中的更改来更新现有数据库。 (Hibernate 在这方面做得非常好,并且使更新和大量修改模型并将其应用到现有数据库变得非常容易。)有没有一种方法可以在不擦除的情况下做到这一点每次都查数据库?每次更改我想要使用的模型后,都必须重新生成管理员用户和网站,这已经变得非常了。

Possible Duplicate:
update django database to reflect changes in existing models

I've used Django in the past and one of the frustrations I've had with it as an ORM tools is the inability to update an existing database with changes in the model. (Hibernate does this very well and makes things really easy for updating and heavily modifying a model and applying this to an existing database.) Is there a way to do this without wiping the database every time? It gets really old having to regenerate admin users and sites after every change in the model which I'd like to play with.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

口干舌燥 2024-12-04 11:27:48

你会想要看看南方。它提供了一个迁移系统,用于将架构更改以及数据从一个版本迁移到下一个版本。

它非常强大,并且绝大多数更改都可以通过简单地处理。

manage.py schemamigration --auto
manage.py migrate

自动功能确实有其限制,特别是如果更改最终将在生产系统上运行,您应该检查代码 --auto< /code> 生成以确保它按照您的预期进行。

South 有一个很好的入门指南,并且有详细的记录。您可以在 http://south.aeracode.org 找到它

You will want to look into South. It provides a migrations system to migrate both schema changes as well as data from one version to the next.

It's quite powerful and the vast majority of changes can be handled simple by going

manage.py schemamigration --auto
manage.py migrate

The auto functionality does have it limits, and especially if the change is going to be run on a production system eventually you should check the code --auto generated to be sure it's doing what you expect.

South has a great guide to getting started and is well documented. You can find it at http://south.aeracode.org

向地狱狂奔 2024-12-04 11:27:48

否。

syncdb 的文档命令指出:

Syncdb 不会更改现有表

syncdb只会创建表
对于尚未安装的型号。永远不会发出
ALTER TABLE 语句以匹配之后对模型类所做的更改
安装。经常更改模型类和数据库模式
涉及某种形式的歧义,在这些情况下,Django 会
猜测要做出的正确更改。存在严重的风险
在此过程中数据将会丢失。

如果您对模型进行了更改并希望更改数据库
要匹配的表,使用sql命令显示新的SQL结构

并将其与现有的表架构进行比较以计算出
变化。

No.

As the documentation of syncdb command states:

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.

If you have made changes to a model and wish to alter the database
tables to match, use the sql command to display the new SQL structure

and compare that to your existing table schema to work out the
changes.

奶茶白久 2024-12-04 11:27:48

South 似乎是大多数人解决此问题的方式,但真正快速且简单的方法是直接通过数据库的交互式 shell 更改数据库。只需启动 db shell(通常只是 dbshel​​l)并使用数据库语法手动更改、添加、删除需要更改的字段和表。

您可能需要运行manage.py sqlall appname来查看Django在创建更新表时将运行的sql语句,然后使用这些语句根据需要更改数据库表和字段。

Django 书中的“更改数据库架构”部分提供了一些如何执行此操作的示例: http://www.djangobook.com/en/1.0/chapter05/

South seems to be how most people solve this problem, but a really quick and easy way to do this is to change the db directly through your database's interactive shell. Just launch your db shell (usually just dbshell) and manually alter, add, drop the fields and tables you need changed using your db syntax.

You may want to run manage.py sqlall appname to see the sql statements Django would run if it was creating the updated table, and then use those to alter the database tables and fields as required.

The Making Changes to a Database Schema section of the Django book has a few examples of how to do this: http://www.djangobook.com/en/1.0/chapter05/

长伴 2024-12-04 11:27:48

我手动进入数据库 - 无论什么可能适合您:MySQL、PostgreSQL 等 - 更改数据库信息,然后我相应地调整 models.py 以供参考。我知道有 Django South,但我不想费心使用另一个第三方应用程序。

I manually go into the database - whatever that may be for you: MySQL, PostgreSQL, etc. - to change database info, and then I adjust the models.py accordingly for reference. I know there is Django South, but I didn't want to bother with using another 3rd party application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文