Skip to content

Conversation

@aare-arnold
Copy link
Contributor

Until v7.35.1 it was possible to define FK indexes between multi tenanted document and single tenanted document in single server configuration. Starting from that version Marten included tenant_id in FK reference to single tenanted document which ended up in PSQL exception:

Npgsql.PostgresException: 42703: column "tenant_id" referenced in foreign key constraint does not exist

SQL that includes invalid FK REFERENCE:

DROP TABLE IF EXISTS public.mt_doc_single_tenanted_doc CASCADE;
CREATE TABLE public.mt_doc_single_tenanted_doc (
    id                  uuid                        NOT NULL,
    data                jsonb                       NOT NULL,
    mt_last_modified    timestamp with time zone    NULL DEFAULT (transaction_timestamp()),
    mt_version          uuid                        NOT NULL DEFAULT (md5(random()::text || clock_timestamp()::text)::uuid),
    mt_dotnet_type      varchar                     NULL,
    mt_created_at       timestamp with time zone    NULL DEFAULT (transaction_timestamp()),
    correlation_id      varchar                     NULL,
    causation_id        varchar                     NULL,
    last_modified_by    varchar                     NULL,
    headers             jsonb                       NULL,
    mt_deleted          boolean                     NULL DEFAULT FALSE,
    mt_deleted_at       timestamp with time zone    NULL,
CONSTRAINT mt_doc_single_tenanted_doc_id PRIMARY KEY (id)
);


DROP TABLE IF EXISTS public.mt_doc_multi_tenanted_doc CASCADE;
CREATE TABLE public.mt_doc_multi_tenanted_doc (
    tenant_id                           varchar                     NOT NULL DEFAULT '*DEFAULT*',
    id                                  uuid                        NOT NULL,
    data                                jsonb                       NOT NULL,
    mt_last_modified                    timestamp with time zone    NULL DEFAULT (transaction_timestamp()),
    mt_version                          uuid                        NOT NULL DEFAULT (md5(random()::text || clock_timestamp()::text)::uuid),
    mt_dotnet_type                      varchar                     NULL,
    mt_created_at                       timestamp with time zone    NULL DEFAULT (transaction_timestamp()),
    correlation_id                      varchar                     NULL,
    causation_id                        varchar                     NULL,
    last_modified_by                    varchar                     NULL,
    headers                             jsonb                       NULL,
    single_tenanted_doc_id              uuid                        NULL,
CONSTRAINT pkey_mt_multi_tenanted_doc_tenant_id_id PRIMARY KEY (tenant_id, id)
);

ALTER TABLE public.mt_doc_multi_tenanted_doc
ADD CONSTRAINT mt_doc_multi_tenanted_doc_tenant_id_single_tenanted_doc_id_fkey FOREIGN KEY(tenant_id, single_tenanted_doc_id)
REFERENCES public.mt_doc_single_tenanted_doc(tenant_id, id);

This PR aims to fix the issue.

@jeremydmiller jeremydmiller changed the base branch from master to 7.0 May 22, 2025 13:53
@jeremydmiller jeremydmiller changed the base branch from 7.0 to master May 22, 2025 13:53
@jeremydmiller jeremydmiller added this to the Marten 8.0 milestone May 22, 2025
@jeremydmiller jeremydmiller changed the base branch from master to 7.0 May 26, 2025 12:09
@jeremydmiller jeremydmiller changed the base branch from 7.0 to master May 26, 2025 12:09
@jeremydmiller
Copy link
Member

Sigh, I'm going to pull this into 8.0 shortly.

@jeremydmiller jeremydmiller merged commit c11d76f into JasperFx:master May 28, 2025
0 of 3 checks passed
@jeremydmiller
Copy link
Member

And this caused a regression problem, but nothing is released yet, so no harm, no foul

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

Successfully merging this pull request may close these issues.

2 participants