Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use child default pkey when inserting in single inheritance #34

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pirogue/single_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ def __insert_trigger(self) -> str:
self.parent_table,
remove_pkey=False,
coalesce_pkey_default=True,
coalesce_pkey_default_value=default_value(
self.cursor, self.child_schema, self.child_table, self.child_pkey
),
remap_columns={self.parent_pkey: self.ref_parent_key},
inner_defaults=self.inner_defaults,
returning="{ppk} INTO NEW.{prk}".format(
Expand Down
7 changes: 6 additions & 1 deletion pirogue/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def insert_command(
remove_pkey: bool = True,
pkey: str = None,
coalesce_pkey_default: bool = False,
coalesce_pkey_default_value: str = None,
skip_columns: list = [],
comment_skipped: bool = True,
remap_columns: dict = {},
Expand Down Expand Up @@ -186,6 +187,8 @@ def insert_command(
can be manually specified.
coalesce_pkey_default
if True, the following expression is used to insert the primary key: COALESCE( NEW.{pkey}, {default_value} )
coalesce_pkey_default_value
optionally give the default value for the primary key
skip_columns
list of columns to be skipped
comment_skipped
Expand Down Expand Up @@ -259,7 +262,9 @@ def value(col):
)
if coalesce_pkey_default and col == pkey:
return "COALESCE( NEW.{cal}, {pk_def} )".format(
cal=cal, pk_def=default_value(pg_cur, table_schema, table_name, pkey)
cal=cal,
pk_def=coalesce_pkey_default_value
or default_value(pg_cur, table_schema, table_name, pkey),
)
elif col in inner_defaults:
def_col = inner_defaults[col]
Expand Down
12 changes: 6 additions & 6 deletions test/demo_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ CREATE SCHEMA pirogue_test;

CREATE SEQUENCE pirogue_test.id_gen START 101;

CREATE OR REPLACE FUNCTION pirogue_test.generate_id(table_name text)
CREATE OR REPLACE FUNCTION pirogue_test.generate_id(number integer)
RETURNS integer AS
$BODY$
DECLARE
BEGIN
RETURN 1000 + nextval('pirogue_test.id_gen');
RETURN number + nextval('pirogue_test.id_gen');
END;
$BODY$
LANGUAGE plpgsql;

CREATE TABLE pirogue_test.animal (
aid integer PRIMARY KEY default pirogue_test.generate_id('animal'),
aid integer PRIMARY KEY default pirogue_test.generate_id(1000),
name text,
year smallint);

Expand All @@ -26,19 +26,19 @@ CREATE TABLE pirogue_test.dog_breed ( id integer PRIMARY KEY, breed_name text );
CREATE TABLE pirogue_test.vet ( id integer PRIMARY KEY, vet_name text );

CREATE TABLE pirogue_test.cat (
cid integer REFERENCES pirogue_test.animal,
cid integer default pirogue_test.generate_id(2000) REFERENCES pirogue_test.animal,
fk_breed integer REFERENCES pirogue_test.cat_breed,
fk_vet integer REFERENCES pirogue_test.vet,
eye_color text); -- not in top class, as some animals might not have eyes

CREATE TABLE pirogue_test.dog (
did integer REFERENCES pirogue_test.animal,
did integer default pirogue_test.generate_id(3000) REFERENCES pirogue_test.animal,
fk_breed integer REFERENCES pirogue_test.dog_breed,
eye_color text);

-- ref col has same name as parent pkey column
CREATE TABLE pirogue_test.aardvark (
aid integer REFERENCES pirogue_test.animal,
aid integer default pirogue_test.generate_id(4000) REFERENCES pirogue_test.animal,
father text
-- ! if adding new fields here, complete the list on test_multiple_inheritance.test_merge_no_columns ! --
);
Expand Down
4 changes: 2 additions & 2 deletions test/test_simple_inheritance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ RESULT=$(psql ${PSQL_ARGS} -c "SELECT COUNT(*) FROM pirogue_test.vw_animal_cat;"
EXPECTED=0
if [[ ${RESULT} =~ "${EXPECTED}" ]]; then echo "ok"; else echo "*** ERROR expected result: ${EXPECTED} got ${RESULT}" && ERROR=1; fi

echo "test insert without pkey value (getting default from parent table)"
echo "test insert without pkey value (getting default from child table)"
psql --quiet -v ON_ERROR_STOP="on" -c "INSERT into pirogue_test.vw_animal_cat (fk_breed, eye_color, name, year) VALUES ('1', 'yellow', 'ninja', 1934);"
RESULT=$(psql ${PSQL_ARGS} -c "SELECT cid FROM pirogue_test.vw_animal_cat")
EXPECTED=1101
EXPECTED=2101
if [[ ${RESULT} =~ "${EXPECTED}" ]]; then echo "ok"; else echo "*** ERROR expected result: ${EXPECTED} got ${RESULT}" && ERROR=1; fi

echo "test on table with same pkey/ref name"
Expand Down