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

schema diff: foreign keys: wrong schema #5488

Closed
fjf2002 opened this issue Nov 2, 2022 · 4 comments
Closed

schema diff: foreign keys: wrong schema #5488

fjf2002 opened this issue Nov 2, 2022 · 4 comments
Assignees
Milestone

Comments

@fjf2002
Copy link

fjf2002 commented Nov 2, 2022

PgAdmin4 Schema Diff generates statements like:

ALTER TABLE IF EXISTS schema1.table1
    ADD CONSTRAINT xxx_fk FOREIGN KEY (yyy)
    REFERENCES schema1.table2 (zzz) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION
    DEFERRABLE;

... but the table2 resides in schema2, not schema1!

@akshay-joshi
Copy link
Contributor

Hi @fjf2002

I have tested the above-mentioned case:
Testing scenario:

  • Create 3 schema (schema1, schema2, schema3)
  • Create a table named "table2" in schema2.
  • Create a table named "table1" in schema1 with a foreign key constraint referring to the column of schema2.table2.
  • Create an almost identical table named "table1" in schema3 except the difference is the foreign key constraint.
  • Compare schema1 and schema3 using the Schema Diff tool.

Found no issues, please refer to the screenshot.

Schema_Diff

@fjf2002
Copy link
Author

fjf2002 commented Nov 8, 2022

(cannot test currently due to #5507)

@fjf2002
Copy link
Author

fjf2002 commented Nov 16, 2022

Working on your schema diff is quite cumbersome. It produces quite sophisticated, hard-to-reproduce errors, and I am not aware of automating that, resulting in mouse click orgies for me. Any suggestions to ease my work?

By the way, I always suspect the schema diff compares object ids, which can fail if I use different servers. Perhaps there is the problem?

Kind of minimal example:

Setup up two brand new postgres instances and a pgadmin instance (I did that via docker containers).

In the first postgres instance, execute

CREATE TABLE foo (i int);
CREATE TABLE bar (j int);

CREATE SCHEMA schema1;
CREATE SCHEMA schema2;
CREATE TABLE schema1.a (
    t text PRIMARY KEY
);
CREATE TABLE schema2.b (
    t text,
    CONSTRAINT fk FOREIGN KEY (t) REFERENCES schema1.a(t)
);

In the second postgres instance, execute

CREATE SCHEMA schema1;
CREATE SCHEMA schema2;
CREATE TABLE schema1.a (
    t text PRIMARY KEY
);
CREATE TABLE schema2.b (
    t text
);

The schema diff of the two servers will incorrectly suggest

ALTER TABLE IF EXISTS schema2.b
    ADD CONSTRAINT fk FOREIGN KEY (t)
    REFERENCES schema2.a (t) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION;

On execution, this will result in:

ERROR:  relation "schema2.a" does not exist
SQL state: 42P01

@akshay-joshi akshay-joshi assigned akshay-joshi and unassigned fjf2002 Nov 23, 2022
@akshay-joshi akshay-joshi moved this to 🆕 New in Current Sprint (183) Nov 23, 2022
@akshay-joshi akshay-joshi added this to the 6.17 milestone Nov 23, 2022
@akshay-joshi akshay-joshi moved this from 🆕 New to 🏗 In Progress in Current Sprint (183) Nov 28, 2022
akshay-joshi added a commit that referenced this issue Nov 28, 2022
@akshay-joshi akshay-joshi moved this from 🏗 In Progress to In Testing in Current Sprint (183) Nov 28, 2022
@FaharAbbasRizvi
Copy link
Contributor

This is resolved in the latest snapshot build:
https://www.postgresql.org/ftp/pgadmin/pgadmin4/snapshots/2022-11-29/

@FaharAbbasRizvi FaharAbbasRizvi moved this from In Testing to ✅ Done in Current Sprint (183) Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants