使用 psycopg2 添加列无法识别类型



我想添加一个带有psycopg2的列,但我遇到了一个错误,说它无法识别类型。我做错了什么?

这是代码:

alter_query_for_attributes =
"""
ALTER TABLE {schema}.{table}
ADD COLUMN IF NOT EXISTS {sat_attributes} {sat_value};
"""
final_query_attributes = sql.SQL(alter_query_for_attributes).format(
schema = sql.Identifier("testSchema"),
table = sql.Identifier("address"),
sat_attributes=sql.Identifier(sat[0]),
sat_value=sql.Identifier(sat[1])
)
ALTER TABLE "testSchema"."address"
ADD COLUMN IF NOT EXISTS "address_line_1" "varchar(250)";

这给了我错误:

psycopg2.errors.UndefinedObject: type "varchar(250)" does not exist
LINE 12: ...        ADD COLUMN IF NOT EXISTS "address_line_1" "varchar(2...
^
"varchar(250)"

被视为数据类型的确切名称,并且没有名为"varchar(250)"的类型。 如果要使用双引号,则大小修饰符需要放在引号之外,"varchar"(250). 或者更好的是,如果您知道不需要引号,请删除引号。您的"sat"元组可能应该有 3 个字段,名称、类型和可选的类型修饰符。

最新更新