Simplify the operation by using a single SQL statement for both inserts using a data-modifying CTE. This is much faster than storing intermediary states in the client.
The INSERT
in the child table only happens if the first INSERT
in the parent table is successful and returns an id
:
void prepare_write_both_tables(connection_base &c){
try
{
c.prepare("write_both_tables",
"WITH p AS ("
"INSERT INTO parent_table (column_1) "
"SELECT $1 "
"RETURNING id) "
"INSERT INTO child_table (parent_table_id, column_a) "
"SELECT p.id, $2 "
"FROM p"
)
("character", pqxx::prepare::treat_string)
("character", pqxx::prepare::treat_string);
}
catch (const exception &e)
{
cerr << e.what() << endl;
}
}
Search for [postgres] & "data-modifying CTE" for more examples.
Also called "writable CTE" (or "writeable CTE").
Multiple children
For a single parent and 0 to many children:
void prepare_write_both_tables(connection_base &c){
try
{
c.prepare("write_both_tables",
"WITH p AS ("
"INSERT INTO parent_table (column_1) "
"SELECT $1 "
"RETURNING id) "
"INSERT INTO child_table (parent_table_id, column_a) "
"SELECT p.id, a "
"FROM p, unnest($2::text[]) AS a"
)
("character", pqxx::prepare::treat_string)
("character", pqxx::prepare::treat_string);
}
catch (const exception &e)
{
cerr << e.what() << endl;
}
}
Where The second parameter is an array of text
in text representation. Example:
{foo,bar,baz}
This inserts as many rows as there are elements in the text array. For 0 children pass NULL
or an empty array {}
.