Foreign key constraints are not copied when using
create table table_name ( like source_table INCLUDING ALL)'
in Postgres. How can I create a copy of an existing table including all foreign keys.
Foreign key constraints are not copied when using
create table table_name ( like source_table INCLUDING ALL)'
in Postgres. How can I create a copy of an existing table including all foreign keys.
There is no option to automatically create foreign keys in
CREATE TABLE ... LIKE ...
.For the documentation:
In practice it's easy with GUI tools. For example, in PgAdmin III:
source_table
to query tool (ctrl-e),In an SQL script you can use the following function. Important assumption: source table foreign keys have correct names i.e. their names contain source table name (what is a typical situation).
Example of use:
One more way is to dump the table structure, change it's name in dump, and load it again: