我使用PostgreSQL在SQL中创建了一个名为"tenants“的表。以下是租户的代码:
create table tentants (
id bigserial not null primary key,
tenant_name varchar(1000) not null,
offices int not null,
number int not null,
email varchar(1000)我想包括在租户租用多个办公室的情况下为" office“添加多个值的功能。我不想为此使用JSON。我尝试创建一个名为“office”的相关表,但这只能让我为每个租户添加一个办公室。解决这一问题的最佳方法是什么?
发布于 2021-10-07 10:38:30
您可以创建一个"tenant_offices"表(与前面一样),其结构为:id, tenant_id, office_id,...,其中id是"tenant_offices"表的主键代码,而tenant_id和office_id是代码外键<>e29>。
引用tenants表的tenant_id和引用offices表的office_id。
因此,在这里,租户可以租用几个办公室。
希望能对您有所启发,或有所帮助!
发布于 2021-10-07 10:39:37
您可以使用text,它适用于我用逗号分隔的ids,如下所示
4,3,67,2无论如何,正确的方法是使用另一个表,并将其命名为tenant_offices
tenant_offices
columns >
tenant_id
office_id (well ofcourse you should have atleast an office table)发布于 2021-10-07 10:39:47
我假设这种关系是一对多租户对办公室(即,办公室只能由一个租户租用)
然后,您必须创建带有指向租户的外键的表office:
CREATE TABLE offices (
id bigserial not null primary key,
tenant_id bigserial foreign key references tenants(id))
additional columns if needed请注意,在此版本中,您不会保留租赁历史记录(您必须在offices上运行更新才能更改tenant_id)
编辑:在多对多关系的情况下(这也将允许我们保留租赁历史),我们需要创建一个关系表:
CREATE TABLE TenantsOffices (
id bigserial not null primary key
tenant_id bigserial foreign key references tenants(id),
office_id bigserial foreign key references offices(id),
start_date datetime,
end_date datetime)https://stackoverflow.com/questions/69479553
复制相似问题