Django Postgresql删除列默认为移植 [英] Django Postgresql dropping column defaults at migrate

查看:97
本文介绍了Django Postgresql删除列默认为移植的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临表格默认值的问题.

i'm facing an issue with defaults values of tables.

例如我有这个模型:

class model1(models.Model):
        field1 = models.CharField(max_length=50, default='My Default Value 1',db_column='field1')
        field2 = models.CharField(max_length=10)

        class Meta:
            db_table = 'model1'

,并且生成的postgresql表没有默认值:

and the postgresql table is generated without the default:

mydb=# \d model1
                                Table "public.model1"
 Column |         Type          |                      Modifiers                      
--------+-----------------------+-----------------------------------------------------
 id     | integer               | not null default nextval('model1_id_seq'::regclass)
 field1 | character varying(50) | not null
 field2 | character varying(10) | not null
Indexes:
    "model1_pkey" PRIMARY KEY, btree (id)

migrate sqlmigrate的输出是:

the output of migrate sqlmigrate is:

CREATE TABLE "model1" ("id" serial NOT NULL PRIMARY KEY, "field1" varchar(50) NOT NULL, "field2" varchar(10) NOT NULL);
COMMIT;

但是如果我修改模型中的默认值并运行makemigrations/migrate,然后在查看sqlmigrations之后,输出将在创建后生成一个奇怪的DROP DEFAULT

but if i modify the default value in the model and run a makemigrations/migrate and after that i look into sqlmigrations the output genereates a weird DROP DEFAULT after creating

ALTER TABLE "model1" ALTER COLUMN "field1" SET DEFAULT 'My Default Value 1 modified';
ALTER TABLE "model1" ALTER COLUMN "field1" DROP DEFAULT;
COMMIT;

是否缺少某些内容或不支持列的默认值?

is there something I am missing or default values for columns are just not supported?

推荐答案

我知道有些晚了,但是我遇到了同样的问题.经过广泛搜索,我发现了这个

I know it's a bit late, but I was having the same issue. After searching extensively, i found this

" Django使用数据库默认值在 桌子.它不会在数据库中保留默认值,因此删除 默认是正确的行为."

"Django uses database defaults to set values on existing rows in a table. It doesn't leave the default values in the database so dropping the default is correct behavior."

https://code.djangoproject.com/ticket/28000

所以看起来这只是django的设计方式.这让我感到困惑,因为当我随后使用MySQL插入数据时,系统会警告我没有默认值.

so it looks like this is simply how django has been designed. It bugged me, because when I then inserted data using MySQL, I was given warnings about having no default.

叹息,我敢肯定,比我聪明的人可以为他们为什么如此设计提供一个很好的解释.

Sigh, I'm sure smarter people than me could come up with a good explanation as to why they've designed it like this.

这篇关于Django Postgresql删除列默认为移植的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆