gpt4 book ai didi

python - 将具有默认值的 Django 字段迁移到 PostgreSQL 数据库

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:37 28 4
gpt4 key购买 nike

https://docs.djangoproject.com/en/1.10/topics/migrations/

这里说:“就模式支持而言,PostgreSQL 是这里所有数据库中功能最强大的;唯一需要注意的是,添加具有默认值的列将导致完全重写表,持续时间与其大小成正比。

“因此,建议您始终使用 null=True 创建新列,因为这样它们会立即添加。”

我在问我是否正确。据我了解,我应该首先使用 null=True 创建字段并且没有默认值然后迁移它然后给它一个默认值并再次迁移它,这些值将立即添加,否则整个数据库将被重写,而 Django 迁移不想自己完成这个技巧?

最佳答案

在同一页中还提到:

In addition, MySQL will fully rewrite tables for almost every schemaoperation and generally takes a time proportional to the number ofrows in the table to add or remove columns. On slower hardware thiscan be worse than a minute per million rows - adding a few columns toa table with just a few million rows could lock your site up for overten minutes.

SQLite has very little built-in schema alteration support, and soDjango attempts to emulate it by:

Creating a new table with the new schema Copying the data acrossDropping the old table Renaming the new table to match the originalname

所以简而言之,你上面提到的那句话真正说的是

postgresql exhibits mysql like behaviour when adding a new column witha default value

您尝试的方法会奏效。添加带有空值的列将意味着不会重写表。然后您可以将该列更改为具有默认值。但是,现有的 null 将继续为 null

关于python - 将具有默认值的 Django 字段迁移到 PostgreSQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41502331/

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