Python Psycopg2创建动态ID并导入JSON文件

发布于 2025-02-10 22:05:41 字数 1451 浏览 0 评论 0原文

我发现了错误“列”中的“ null值”中的“ null值”违反了“无效约束” 导入到psycopg2之后。可以创建动态ID,然后在Psycopg中导入JSON文件?因为我的JSON文件没有ID,并且不允许将ID放入JSON中。

我创建了这样的表:

create table item_free(
        "id" serial NOT NULL,
        "item_1" varchar(2000),
        "item_2" varchar(2000),
        "item_3" varchar(2000),
        "item_4" varchar(2000),
        "item_5" varchar(2000),
        "item_6" varchar(2000),
        "item_7" varchar(2000),
        "item_8" varchar(2000),
        "item_9" varchar(2000),
        "item_10" varchar(2000),
        PRIMARY KEY ( "id" )
);

SELECT setval(pg_get_serial_sequence('item_free', 'id'), COALESCE(max(id)+1, 1), FALSE) FROM item_free;

我的JSON文件看起来像:

{
    "item_1": "Test",
    "item_2": null,
    "item_3": "Sample",
    "item_4": null,
    "item_5": "Milk,
    "item_6": null,
    "item_7": "Drinks",
    "item_8": null,
    "item_9": null,
    "item_10": null
},

这是我的代码导入JSON文件:

with psycopg2.connect(host='0.0.0.0', port='0000' ,dbname='Test', user='TEST', password='TEST') as conn:
    with conn.cursor() as cur:
        with open('../json/file/Location/file.json') as my_file:
            data = json.load(my_file)
           ********Some code here ***
        query_sql = """ insert into item_free
            select * from json_populate_recordset(NULL::item_free, %s) """
        cur.execute(query_sql, (json.dumps(data),))

I got and error "null value in column "id" violates not-null constraint" after import json file to psycopg2. It's possible to create dynamic ID then import json file in psycopg? because my json file have no ID and not allow to put ID's in json.

I created table like this:

create table item_free(
        "id" serial NOT NULL,
        "item_1" varchar(2000),
        "item_2" varchar(2000),
        "item_3" varchar(2000),
        "item_4" varchar(2000),
        "item_5" varchar(2000),
        "item_6" varchar(2000),
        "item_7" varchar(2000),
        "item_8" varchar(2000),
        "item_9" varchar(2000),
        "item_10" varchar(2000),
        PRIMARY KEY ( "id" )
);

SELECT setval(pg_get_serial_sequence('item_free', 'id'), COALESCE(max(id)+1, 1), FALSE) FROM item_free;

And my json file look like:

{
    "item_1": "Test",
    "item_2": null,
    "item_3": "Sample",
    "item_4": null,
    "item_5": "Milk,
    "item_6": null,
    "item_7": "Drinks",
    "item_8": null,
    "item_9": null,
    "item_10": null
},

This is my code to import json file:

with psycopg2.connect(host='0.0.0.0', port='0000' ,dbname='Test', user='TEST', password='TEST') as conn:
    with conn.cursor() as cur:
        with open('../json/file/Location/file.json') as my_file:
            data = json.load(my_file)
           ********Some code here ***
        query_sql = """ insert into item_free
            select * from json_populate_recordset(NULL::item_free, %s) """
        cur.execute(query_sql, (json.dumps(data),))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野心澎湃 2025-02-17 22:05:41

    query_sql = """ insert into item_free
        select * from json_populate_recordset(NULL::item_free, %s) """

更明确地编写了短语:

    query_sql = """ insert into item_free (
            item_1,
            item_2,
            ...
            item_10)
        select * from json_populate_recordset(NULL::item_free, %s) """

id未提及,
因此,它将提供通常的序列默认行为,
产生一个独特的整数。

You wrote

    query_sql = """ insert into item_free
        select * from json_populate_recordset(NULL::item_free, %s) """

Phrase it more explicitly:

    query_sql = """ insert into item_free (
            item_1,
            item_2,
            ...
            item_10)
        select * from json_populate_recordset(NULL::item_free, %s) """

id is not mentioned,
so it will offer the usual serial defaulting behavior,
generating a distinct integer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文