【发布时间】:2021-05-17 01:21:55
【问题描述】:
我们正在使用基于模式的多租户数据库。我们在 Elixir 堆栈上,在 Phoenix 框架项目中使用 Postgres、Ecto 和 Triplex。
我们使用默认架构 public 来存储常见数据,例如用户和组织。特别是,我们在Organisations 表中有一个tenant_prefix 列,用于将用户映射到他们的租户。
在租约中,我们使用特定于租户的表格。例如,我们有一个Products 表。创建新组织时,我们使用 Triplex 创建架构并运行租户迁移,从而创建特定于租户的表,例如 Products。
在视觉上,数据库如下所示:
- app_database
- public
- users
- organisations
- organisations_users
- tenant1
- products
- (other tables...)
- tenant2
- products
- (other tables...)
Products 迁移如下所示。
1 defmodule App.Repo.Migrations.CreateProducts do
2 use Ecto.Migration
3
4 def change do
5 create table(:products) do
6 add :title, :string
7 add :description, :string
8 add :organisation_id, references(:organisations, on_delete: :nothing), null: false
9
10 timestamps()
11 end
12
13 create index(:products, [:organisation_id])
14 end
15 end
现在,由于Line 8,它无法运行。报错为:ERROR 42P01 (undefined_table) relation "59ef85c702d24d0fac5c7e425d0d3d44.organisations" does not exist
租户前缀是一个 UUID。
总而言之,我们想知道如何引用public.organisations 表来定义tenant.products 中的外键关系。
【问题讨论】:
标签: postgresql phoenix-framework ecto