首页 > 解决方案 > 在 postgres 的表中插入嵌套的自定义数组

问题描述

command = (

CREATE TYPE belongings AS (
   item TEXT,
   quantity INTEGER
)

CREATE TYPE student AS ( 
   name TEXT,
   id  INTEGER,
   bag belongings[]
)

CREATE TABLE studentclass( 
    date DATE NOT NULL,
    time TIMESTAMPTZ NOT NULL, 
    PRIMARY KEY (date, time), 
    class student
)
)

我可以问如何在 postgres psycog2 中为此插入吗?谢谢你。

当我将插入作为 insert_sql = "INSERT INTO studentclass (date, time, class) VALUES (%s,%s,%s)"

错误输出为详细信息:无法将类型文本 [] 转换为列中的财物 []

我不认为我只是在 INSERT 语句中使用 "::belongings[]" 进行转换,因为它是嵌套的。

我之前提出的更简单表格的问题。

无法在 postgres 中插入嵌套记录

标签: postgresqlpsycopg2

解决方案


推荐阅读