diff --git a/pirogue/single_inheritance.py b/pirogue/single_inheritance.py index 5a5c315..7ac67f2 100644 --- a/pirogue/single_inheritance.py +++ b/pirogue/single_inheritance.py @@ -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( diff --git a/pirogue/utils.py b/pirogue/utils.py index 5c20226..8e93d80 100644 --- a/pirogue/utils.py +++ b/pirogue/utils.py @@ -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 = {}, @@ -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 @@ -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] diff --git a/test/demo_data.sql b/test/demo_data.sql index 561f7e9..577e279 100644 --- a/test/demo_data.sql +++ b/test/demo_data.sql @@ -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); @@ -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 ! -- ); diff --git a/test/test_simple_inheritance.sh b/test/test_simple_inheritance.sh index 1f293a1..34b6b79 100755 --- a/test/test_simple_inheritance.sh +++ b/test/test_simple_inheritance.sh @@ -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"