Python 3.x 从值插入外键id

Python 3.x 从值插入外键id,python-3.x,postgresql,psycopg2,Python 3.x,Postgresql,Psycopg2,我有两张桌子 Table "public.part" Column | Type | -------------------+------------------------+ id | bigint | part_name | character varying(255) | part_department | chara

我有两张桌子

            Table "public.part"
      Column       |          Type          |
-------------------+------------------------+
 id                | bigint                 | 
 part_name         | character varying(255) |
 part_department   | character varying(255) |
 part_manufacturer | character varying(255) | 
 part_model        | character varying(255) |
 part_category     | character varying(255) | 
 part_storage      | character varying(255) | 
 part_description  | character varying(255) | 
 part_stocklimit   | smallint               | 


     Table "public.entry"
     Column     |          Type          |
----------------+------------------------+
 id             | bigint                 | 
 part_id        | bigint                 |
 entry_status   | character(5)           |
 entry_qty      | smallint               |
 entry_desc     | character varying(500) |
 entry_date     | date                   |
 entry_employee | character varying(225)
我想在条目表中插入数据。条目表具有part_id,它是part表中id的外键

我构建了一个from,使用PyQt5插入条目表的值,并使用psycopg2在python中运行psql命令,如下所示

名称标签具有组合框,用于从零件表中获取零件列表。现在,我想插入组合框中与零件表中id匹配的数据。 让我们这样说吧

INSERT INTO entry (part_id, entry_status, entry_qty, entry_desc, entry_date, entry_employee) VALUES
( SELECT id FROM part WHERE part_name=entry_part_name, entry_status, entry_qty, entry_desc, entry_date, entry_employee );
现在我已经用python编写了标准的INSERT语句,如下所示。但是我不知道如何匹配零件的id,以便它可以插入id而不是零件名称

    def AddEntry_Data(self):
    self.db = ConnectDatabase()
    entry_part_name = self.comboBoxNewEntryPart.currentText()
    entry_employee = self.comboBoxNewEntryEmployee.currentText()
    entry_status = self.comboBoxNewEntryStatus.currentText()
    entry_quantity = self.spinBoxNewEntryQuantity.value()
    entry_description = self.lineNewEntryDescription.toPlainText()
    entry_date = self.dateNewEntryDate.text()

    try:
        self.cur = self.db.cursor()
        self.cur.execute('SAVEPOINT SP1')
        self.cur.execute('''INSERT INTO entry (part_id, entry_status, entry_qty, entry_desc, entry_date, entry_employee) 
                                            VALUES (%s , %s , %s , %s , %s , %s , %s , %s)''', (entry_part_name, entry_status, entry_quantity, entry_description, entry_date, entry_employee))
    except Exception as error:
        self.cur.execute('ROLLBACK TO SAVEPOINT SP1')
        self.AddEntry_reset()
        print(error)
        # self.error_popup("Input Error", error)
    else:
        self.cur.execute('RELEASE SAVEPOINT SP1')
        self.db.commit()
        self.AddEntry_reset()
        self.success_popup("Entry", "New Entry Added.")
    self.db.close
我可以想到的一种方法是选择查询id并存储在变量中,然后将变量传递给insert查询

        try:
        self.cur = self.db.cursor()
        self.cur.execute('SAVEPOINT SP1')
        self.cur.execute('''SELECT id FROM part WHERE part_name = %s ''', [(entry_part_name,)])
    except Exception as error:
        self.cur.execute('ROLLBACK TO SAVEPOINT SP1')
        self.AddEntry_reset()
        self.error_popup("Input Error", error)
    else:
        fetched_part_id = self.cur.fetchone()
        try:
            self.cur.execute('''INSERT INTO entry (fetched_part_id, entry_status, entry_qty, entry_desc, entry_date, entry_employee)
                                                VALUES (%s , %s , %s , %s , %s , %s)''', (fetch_id, entry_status, entry_quantity, entry_description, entry_date, entry_employee))
        except Exception as error:
            self.cur.execute('ROLLBACK TO SAVEPOINT SP1')
            self.AddEntry_reset()
            self.error_popup("Input Error", error)
        else:
            self.cur.execute('RELEASE SAVEPOINT SP1')
            self.db.commit()
            self.AddEntry_reset()
            self.success_popup("Entry", "New Entry Added.")
    self.db.close()

如果有任何其他简单的方法可以将select查询与python中的insert查询相结合,请使用顶部的wise sql命令。谢谢。

您可以将
插入到…
选择组合起来:

sql = """INSERT INTO entry (part_id, entry_status, entry_qty, entry_desc, entry_date, entry_employee) VALUES ((SELECT id FROM part WHERE part_name = %s), %s , %s , %s , %s , %s)"""

self.cur.execute(sql, (entry_part_name, entry_status, entry_quantity, entry_description, entry_date, entry_employee))

非常感谢。工作得很有魅力@莫里斯·迈耶