Django手动数据库迁移

发布于 2024-09-24 08:02:25 字数 647 浏览 0 评论 0原文

我更喜欢手动迁移 Django 中的表。因为使用自动化工具让我无法看到影响。影响,我的意思是数据库与我的模型同步所需的时间。下面是一个简单的示例:

class User(models.Model):
   first_name = CharField(..)

假设我想添加此内容:

class User(models.Model):
       first_name = CharField(..)
       last_name = CharField(..)

我将在生产服务器中执行以下步骤:

  1. 禁用站点流量。
  2. 手动连接到您的数据库服务器(假设是 MySQL)并向名为 last_name 的用户表添加一个字段(当然,请确保它与为新模型生成的 SQL 同步。)
  3. 更新您的模型。
  4. 上传新文件,重新启动流量。

对于这种情况,我有两个问题:

  1. 这是 Django 中手动数据库迁移的首选/可接受的方式吗?
  2. 如果我只是通过 SQL 手动向 User 表添加具有特定默认值的字段,但不更新模型,我还会遇到 DatabaseIntegrity 异常吗?

提前致谢,

I am preferring to manually migrate my tables in Django. Because using automated tools puts me in a place where I cannot see the impact. With impact, I mean the the time it takes the db get in synch with my models. Below is a simple example:

class User(models.Model):
   first_name = CharField(..)

Let's say I want to add this:

class User(models.Model):
       first_name = CharField(..)
       last_name = CharField(..)

I will follow the these steps in my production server:

  1. Disable site traffic.
  2. Manually connect to the your DB server, let's say MySQL and add a field to the User table named last_name (make sure it is sync with the SQL generated for the new Model, of course.)
  3. Update your model.
  4. Upload new files, restart traffic.

I have two questions for this scenario:

  1. Is this a preferred/acceptable way for manual db migration in Django?
  2. If I just add a field with a specific default value to the User table by SQL manually, but don't update the model, will I still get DatabaseIntegrity exception?

Thanks in advance,

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

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

发布评论

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

评论(2

粉红×色少女 2024-10-01 08:02:25

使用所有架构迁移工具(例如 south),可以通过多种方法显式定义模型的迁移方式。使用这样的工具的好处是:

  • 您的迁移存储在版本控制系统中
  • 有一个回滚架构迁移的记录程序
  • 如果另一个开发人员加入您的项目,您可以将该人推荐给南方文档,而不是解释您自己的文档记录模式迁移的黑客解决方案。

我想我应该在这里强调一点:虽然south有自动迁移工具,但是如果你使用South,则不必使用自动迁移

With all of the schema migration tools, such as south, there are ways of explicitly defining how your models get migrated. The benefits of using a tool such as this are:

  • Your migrations are stored in your version control system
  • There's a documented procedure to roll back schema migrations
  • If another developer joins your project, you can refer that person to the south documentation rather than explaining your own hacky solution to documenting schema migrations.

I think I should just emphasize a point here: Though south has automigration tools, you don't have to use automigration if you're using South.

他夏了夏天 2024-10-01 08:02:25

这是 Django 中手动数据库迁移的首选/可接受的方式吗?

我会回答不。正如 @Mike 所说,Django 拥有一个可靠且相当通用的迁移工具生态系统,其中最突出的是 South。 @Mike的答案有正确的细节。

回答你的第二个问题:

如果我只是通过 SQL 手动向 User 表添加具有特定默认值的字段,但不更新模型,我还会遇到 DatabaseIntegrity 异常吗?

不会。您的模型将继续正常运行。当然,如果您想使用 Django 的 ORM 对新字段执行某些操作,最好将它们添加到模型类中。

这样做的一个副作用是,您可以通过有选择地选择要在模型中使用的字段来迁移旧数据库表。

Is this a preferred/acceptable way for manual db migration in Django?

I would answer no. As @Mike said Django has a reliable and fairly versatile ecosystem of migration tools, the most prominent of which is South. @Mike's answer has the details right.

To answer your second question:

If I just add a field with a specific default value to the User table by SQL manually, but don't update the model, will I still get DatabaseIntegrity exception?

No. Your models will continue to function normally. Of course if you want to do something with the new fields using Django's ORM you'll be better off adding them to the model class.

A side effect of this is that you can migrate legacy database tables by selectively choosing the fields to use in your models.

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