gpt4 book ai didi

django - 如何更改 PostgreSQL 中的列类型和所有外键?

转载 作者:行者123 更新时间:2023-11-29 11:53:49 25 4
gpt4 key购买 nike

我的 Django 应用程序目前陷入困境 22。我必须将列的类型从 Varying char 更改为 Integer,因为我正在从 UUID 移动到普通的旧 ID(数据不会改变,因为它是物理常量)。现在 django 扔了一个 Fit 最初关于不能从 VarChar 转换为 Int,所以我“帮助”了它:

 ALTER TABLE glass_fill ALTER COLUMN id TYPE INTEGER USING CAST(id AS INT);

现在,它说:

django.db.utils.DatabaseError: foreign key constraint "glass_fill_manufacturer_glass_fill_id_fkey" cannot be implemented
DETAIL: Key columns "glass_fill_id" and "id" are of incompatible types: integer and character varying.

有什么想法吗?

注意:尚未创建 glass_fill_manufacturer 表,但 django 尝试在 syncdb 上创建但失败。还有,

ALTER TABLE glass_fill ALTER COLUMN id TYPE INTEGER USING CAST(id AS INT); 

行并没有像我想的那样改变列。

glass_fill 表架构:

-- Table: glass_fill

-- DROP TABLE glass_fill;

CREATE TABLE glass_fill
(
id character varying(36) NOT NULL,
name character varying(255),
temperature real,
density real,
viscosity real,
conductivity real,
heat_capacity real,
colour text,
CONSTRAINT glass_fill_pkey PRIMARY KEY (id )
)
WITH (
OIDS=FALSE
);

最佳答案

ALTER TABLE glass_fill_manufacturer 
ALTER COLUMN glass_fill_id TYPE INTEGER USING CAST(glass_fill_id AS INT)
;

根据你的文字,我猜 Django 会以某种方式做到这一点,但如果它没有做到:

ALTER TABLE glass_fill_manufacturer 
drop constraint glass_fill_manufacturer_glass_fill_id_fkey
;

alter table glass_fill_manufacturer
ADD constraint glass_fill_manufacturer_glass_fill_id_fkey
foreign key (glass_fill_id) references glass_fill (id)
;

关于django - 如何更改 PostgreSQL 中的列类型和所有外键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10753409/

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