【问题标题】:Add column with psycopg2 doesn't recognise type使用 psycopg2 添加列无法识别类型
【发布时间】:2020-01-13 13:24:39
【问题描述】:

我想用 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...
                                                          ^

【问题讨论】:

  • 报错是因为最终的sql查询有双引号中的数据类型。检查这个:dbfiddle.uk/…

标签: python sql postgresql psycopg2


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多