使用 psycopg2 插入带有串行列的 table

Inserting into a table with a serial column using psycopg2

我在 PostgreSQL 中有两个简单的 tables,是我通过 pgAdmin4 创建的:

表 1:

CREATE TABLE public.table1
(
    id serial,
    name text,
    PRIMARY KEY (id)
)

表 2:

CREATE TABLE public.table2
(
    fk_id integer,
    name text,
    PRIMARY KEY (name),
    CONSTRAINT fk FOREIGN KEY (fk_id)
        REFERENCES public.table1 (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
)

在使用引用第一个 table 的外键创建第二个 table 之后,来自 table1 的数据类型 ID 自动更改为整数:

如果我尝试手动将类型更改为串行,则找不到。

并且还使用 psycopg2 此插入有效:

connection = psycopg2.connect(user = "user",
                              password = "pass",
                              host = "localhost",
                              port = "5432",
                              database = "db")

cursor = connection.cursor()

postgres_insert_query = """INSERT INTO table1 (id,name) VALUES (%s,%s)"""
record_to_insert = (1,'sometext')
cursor.execute(postgres_insert_query, record_to_insert)

connection.commit()

但是这个失败了:

postgres_insert_query = """INSERT INTO table1 (name) VALUES (%s)"""
record_to_insert = ('sometext')

打印此错误:"not all arguments converted during string formatting"

我只是漏掉了@Jeremy 评论的逗号

record_to_insert = ('sometext',)