Last active
May 1, 2020 17:17
-
-
Save victorcrbt/8c3a0ed2d6167d82c21d8717b047f1da to your computer and use it in GitHub Desktop.
Conditional PosgreSQL not null column based on value of another table.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION checkClientType(int) RETURNS varchar as $$ | |
DECLARE | |
type varchar; | |
BEGIN | |
SELECT client_type INTO type FROM clients WHERE id = $1; | |
RETURN type; | |
END; | |
$$ LANGUAGE plpgsql; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE aditional_info ( | |
id SERIAL NOT NULL UNIQUE, | |
client_id int REFERENCES clients(id) ON DELETE SET NULL ON UPDATE CASCADE, | |
rg varchar(10) NOT NULL, | |
issuing_body varchar NOT NULL, | |
birth_date timestamp NOT NULL, | |
municipal_registration varchar, | |
CONSTRAINT not_null_municipal_registration CHECK(CASE WHEN checkClientType(client_id) = 'J' THEN municipal_registration IS NOT NULL END) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment