I have an insert on postgres 10 that uses subqueries to look up foreign keys on a unique column, like this (just an illustration example):
CREATE TABLE company
(id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
companyCode CHAR(4) NOT NULL UNIQUE);
CREATE TABLE customer
(id SERIAL PRIMARY KEY,
company NOT NULL REFERENCES company (id),
name VARCHAR(100) NOT NULL);
INSERT INTO customer (name, company) values ('Bill Gates', (select id from company where companyCode='MSFT'));
I'm trying to use the pg-promise NodeJS library to insert multiple rows into the database quickly. The recommended way seems to be to use helpers. However I haven't found anything in the documentation or the examples that refers to using subqueries in this way.
Am I missing an obvious way to do this? Or am I approaching the whole question in the wrong way, and is there a simpler way than pg-promise to generate the bulk insert code while still keeping the injection protection?
Thanks!