Psycopg2 使用外键创建记录
Psycopg2 to create a record using a foreign key
我是运行以下软件版本:
Python 3.5
psycopg2==2.6.1
Postgres 9.4.5
我有2张桌子。 Table User
有 UserId (serial PK)
、LastName
、FirstName
、Gender
、DateOfBirth
和 DateEnrolled
。 Table UserProfile
有 UserProfileId (serial, PK)
、UserId (FK)
、DateEntered
、FaveNumber
和 Activity
。存在一对多关系。
以下 PostgreSQL 起作用并最终在 UserProfile 中创建一条记录,并带有关联的 UserId (FK)。
\set last_name '''Sara'''
\set first_name '''Jackson'''
\set gender '''F'''
\set dob '''1941-1-12'''
\set fave_number '''3'''
\set activity '''volleyball'''
WITH ins_user AS (
INSERT INTO User
(LastName, FirstName, Gender, DateOfBirth, DateEnrolled)
VALUES (:last_name, :first_name, :gender, :dob, now())
RETURNING UserId)
INSERT INTO UserProfile
(UserId, DateEntered, FaveNumber, Activity)
VALUES ( (SELECT UserId FROM ins_user), now(), :fave_number :activity);
我如何构建一个 psycopg2 cur.execute 查询来完成上述 PostgreSQL?我已阅读文档,但似乎无法理解应如何构建此命令。
我的出发点是:
cur.execute( \
"""INSERT INTO User \
(LastName, FirstName, Gender, DateOfBirth, DateEnrolled) \
VALUES (%s, %s, %s, %s, %s) RETURNING UserId;""", \
(last_name, first_name, gender, date_of_birth, now(), ??...??)
此外,是否可以提取从 RETURNING UserId
派生的值,以便在以后的查询中使用它?
感谢您的宝贵时间!
cur.execute( \
"""INSERT INTO User \
(LastName, FirstName, Gender, DateOfBirth, DateEnrolled) \
VALUES (%s, %s, %s, %s, %s) RETURNING UserId;""", \
(last_name, first_name, gender, date_of_birth, now(), ??...??)
使用字典传递参数更清晰
ins = """
with ins_user as (
insert into User (
LastName, FirstName, Gender, DateOfBirth, DateEnrolled
) values (%(last_name)s, %(first_name)s, %(gender)s, %(dob)s, now())
returning UserId
)
insert into UserProfile (
UserId, DateEntered, FaveNumber, Activity
) values ((select UserId from ins_user), now(), %(fave_number)s, %(activity)s)
"""
ins_dict = dict (
last_name = 'Sara',
first_name = 'Jackson',
gender = 'F',
dob = '1941-1-12',
fave_number = '3',
activity = 'volleyball'
)
print cur.mogrify(ins, ins_dict) # inspect the rendered command
cur.execute(ins, ins_dict)
connection.commit()
如果 DateEnrolled
将始终收到 now()
则将其设置为默认值
DateEnrolled date default now(),
我是运行以下软件版本:
Python 3.5
psycopg2==2.6.1
Postgres 9.4.5
我有2张桌子。 Table User
有 UserId (serial PK)
、LastName
、FirstName
、Gender
、DateOfBirth
和 DateEnrolled
。 Table UserProfile
有 UserProfileId (serial, PK)
、UserId (FK)
、DateEntered
、FaveNumber
和 Activity
。存在一对多关系。
以下 PostgreSQL 起作用并最终在 UserProfile 中创建一条记录,并带有关联的 UserId (FK)。
\set last_name '''Sara'''
\set first_name '''Jackson'''
\set gender '''F'''
\set dob '''1941-1-12'''
\set fave_number '''3'''
\set activity '''volleyball'''
WITH ins_user AS (
INSERT INTO User
(LastName, FirstName, Gender, DateOfBirth, DateEnrolled)
VALUES (:last_name, :first_name, :gender, :dob, now())
RETURNING UserId)
INSERT INTO UserProfile
(UserId, DateEntered, FaveNumber, Activity)
VALUES ( (SELECT UserId FROM ins_user), now(), :fave_number :activity);
我如何构建一个 psycopg2 cur.execute 查询来完成上述 PostgreSQL?我已阅读文档,但似乎无法理解应如何构建此命令。
我的出发点是:
cur.execute( \
"""INSERT INTO User \
(LastName, FirstName, Gender, DateOfBirth, DateEnrolled) \
VALUES (%s, %s, %s, %s, %s) RETURNING UserId;""", \
(last_name, first_name, gender, date_of_birth, now(), ??...??)
此外,是否可以提取从 RETURNING UserId
派生的值,以便在以后的查询中使用它?
感谢您的宝贵时间!
cur.execute( \
"""INSERT INTO User \
(LastName, FirstName, Gender, DateOfBirth, DateEnrolled) \
VALUES (%s, %s, %s, %s, %s) RETURNING UserId;""", \
(last_name, first_name, gender, date_of_birth, now(), ??...??)
使用字典传递参数更清晰
ins = """
with ins_user as (
insert into User (
LastName, FirstName, Gender, DateOfBirth, DateEnrolled
) values (%(last_name)s, %(first_name)s, %(gender)s, %(dob)s, now())
returning UserId
)
insert into UserProfile (
UserId, DateEntered, FaveNumber, Activity
) values ((select UserId from ins_user), now(), %(fave_number)s, %(activity)s)
"""
ins_dict = dict (
last_name = 'Sara',
first_name = 'Jackson',
gender = 'F',
dob = '1941-1-12',
fave_number = '3',
activity = 'volleyball'
)
print cur.mogrify(ins, ins_dict) # inspect the rendered command
cur.execute(ins, ins_dict)
connection.commit()
如果 DateEnrolled
将始终收到 now()
则将其设置为默认值
DateEnrolled date default now(),