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

MSSQL -> Postgres: Multi-Column Key Handling #1642

Open
JohnYoungers opened this issue Feb 4, 2025 · 0 comments
Open

MSSQL -> Postgres: Multi-Column Key Handling #1642

JohnYoungers opened this issue Feb 4, 2025 · 0 comments

Comments

@JohnYoungers
Copy link

I have some tables with multiple columns in their primary key like so:

CREATE SCHEMA [MySchema]
GO
CREATE TABLE [MySchema].[MyTable]
(
    [TenantId]                UNIQUEIDENTIFIER  NOT NULL,
    [MyTableId]                   UNIQUEIDENTIFIER  NOT NULL,
    [Name]                       NVARCHAR(200)     NOT NULL,
    CONSTRAINT [PK_MySchema_TableA] PRIMARY KEY CLUSTERED ([TenantId], [MyTableId])
)
CREATE TABLE [MySchema].[MySubTable]
(
    [TenantId]                UNIQUEIDENTIFIER  NOT NULL,
    [MyTableId]                   UNIQUEIDENTIFIER  NOT NULL,
    [MySubTableId]                    UNIQUEIDENTIFIER  NOT NULL,
    [Name]                       NVARCHAR(200)     NOT NULL,
    CONSTRAINT [PK_MySchema_MySubTable] PRIMARY KEY CLUSTERED ([TenantId], [MyTableId], [MySubTableId]),
    CONSTRAINT [FK_MySchema_MySubTable_MyTable] FOREIGN KEY (TenantId, MyTableId) REFERENCES [MySchema].[MyTable] (TenantId, MyTableId)
)
GO

If I run this with or without any options, it will create the primary keys correctly, but for some reason it won't properly group the foreign key results from the MSSQL query, and attempts to create separate foreign keys:

NOTICE CREATE UNIQUE INDEX PK_MySchema_TableA ON my_schema.my_table (tenant_id, my_table_id);
NOTICE CREATE UNIQUE INDEX PK_MySchema_MySubTable ON my_schema.my_sub_table (tenant_id, my_table_id, my_sub_table_id);
NOTICE Completing PostgreSQL database.
NOTICE Reset sequences
NOTICE ALTER TABLE my_schema.my_table ADD PRIMARY KEY USING INDEX PK_MySchema_TableA;
NOTICE ALTER TABLE my_schema.my_sub_table ADD PRIMARY KEY USING INDEX PK_MySchema_MySubTable;
NOTICE ALTER TABLE my_schema.my_sub_table ADD CONSTRAINT fk__my_schema__my_sub_table__my_table FOREIGN KEY(tenant_id) REFERENCES my_schema.my_table(tenant_id) ON UPDATE NO ACTION ON DELETE NO ACTION
ERROR PostgreSQL Database error 42830: there is no unique constraint matching given keys for referenced table "my_table"
QUERY: ALTER TABLE my_schema.my_sub_table ADD CONSTRAINT fk__my_schema__my_sub_table__my_table FOREIGN KEY(tenant_id) REFERENCES my_schema.my_table(tenant_id) ON UPDATE NO ACTION ON DELETE NO 
ACTION
QUERY: ALTER TABLE my_schema.my_sub_table ADD CONSTRAINT fk__my_schema__my_sub_table__my_table FOREIGN KEY(my_table_id) REFERENCES my_schema.my_table(my_table_id) ON UPDATE NO ACTION ON DELETE NO ACTION

I have zero experience in lisp, but tried to follow the code, and it seems like it's setup to build up the foreign key definition columns opposed to creating separate indexes per record returned from MSSQL, but I'm not sure exactly where the issue would be

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant