Django Postgresql删除列默认为移植

编程入门 行业动态 更新时间:2024-10-26 10:35:54
本文介绍了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."

code.djangoproject/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删除列默认为移植

本文发布于:2023-10-17 02:57:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1499571.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:默认为   Django   Postgresql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!