I have 4 tenants – tenant1,tenant2,tenant3,tenant4,tenant5
and i am using spring boot, postgress database and flywaydb Migration to database version and auto create when new tenant registered.
Base code : https://github.com/AnarSultanov/examples/tree/master/schema-based-multi-tenancy
https://sultanov.dev/blog/schema-based-multi-tenancy-with-spring-data/
in my sql scripts i have added foreign key but after flyway db initializes and app runs
all foreign key for each schema/table are referring to all schema
one schema can access another foreign key table reference
Foreign keys are creating number of tenant present
this ll generate the schema by looping from the user/tenant table
after fly migrate run successfully , foreign keys are creating multiple times (same count as schema present).
Correct behavior should create only one foriegn key per schema.
sample code to reproduce the issue
https://github.com/jeebendu/schema-based-multi-tenancy
clone and connect to any postgress db , it ll automatic create the 5 tenants
Correct behavior should be only one foreign key
Tenant_1 added Author_t1
While inserting record in Tenant_3 its refering to tenant1 foriegn key data
Can any one help to this ?
2
Answers
Its not an issue in Flywaydb or Postgress Its a issue on heidiSQL Client
Raised request @ https://github.com/HeidiSQL/HeidiSQL/issues/1956
You could try making all references to tables have the schema in the reference. Flyway allows you to specify a placeholder to help with multi-tenant schemas: https://www.red-gate.com/blog/handling-multiple-schemas-in-the-same-database-with-flyway
Other than that – it isn’t clear what the exact issue is – could you recreate it in a small reproduction that you can share?
I’ve downloaded the code, and added in the FK from author to users.
I believe there is an issue in the SQL Query used to examine the FK’s – the join to ccu didn’t include a condition that the schema’s matched and therefore there were extra rows.
This query seems to show that there are the right number of FK’s – one per tenant back to the users table: