Skip to content

Commit c11d76f

Browse files
aare-arnoldjeremydmiller
authored andcommitted
fix FK reference from multi tenanted document to single tenanted document
1 parent 291a92c commit c11d76f

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

src/DocumentDbTests/ForeignKeys/configuring_foreign_key_fields.cs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2-
using JasperFx.Core;
2+
using System.Linq;
33
using JasperFx.Core.Reflection;
44
using Marten;
55
using Marten.Schema;
6+
using Marten.Storage;
7+
using Marten.Storage.Metadata;
68
using Marten.Testing.Harness;
79
using Shouldly;
810
using Xunit;
@@ -66,6 +68,75 @@ public void should_allow_foreign_key_on_id_field()
6668
.ShouldContain(x => x.ColumnNames[0] == "id");
6769
}
6870

71+
[Fact]
72+
public void should_not_include_tenant_id_in_foreign_key_from_tenanted_doc_to_not_tenanted_doc()
73+
{
74+
var store = StoreOptions(_ =>
75+
{
76+
_.MultiTenantedWithSingleServer(ConnectionSource.ConnectionString, c =>
77+
{
78+
c.WithTenants("tenant1").InDatabaseNamed("postgres");
79+
});
80+
81+
_.Schema.For<Foo>()
82+
.SingleTenanted()
83+
.Identity(x => x.FooId);
84+
_.Schema.For<Bar>()
85+
.MultiTenanted()
86+
.Identity(x => x.BarId)
87+
.ForeignKey<Foo>(x => x.FooId);
88+
});
89+
90+
var mapping = store.Options.Storage.MappingFor(typeof(Bar));
91+
new DocumentTable(mapping).ForeignKeys.Single().ColumnNames.ShouldNotContain(TenantIdColumn.Name);
92+
}
93+
94+
[Fact]
95+
public void should_include_tenant_id_in_foreign_key_between_tenanted_docs()
96+
{
97+
var store = StoreOptions(_ =>
98+
{
99+
_.MultiTenantedWithSingleServer(ConnectionSource.ConnectionString, c =>
100+
{
101+
c.WithTenants("tenant1").InDatabaseNamed("postgres");
102+
});
103+
104+
_.Schema.For<Foo>()
105+
.MultiTenanted()
106+
.Identity(x => x.FooId);
107+
_.Schema.For<Bar>()
108+
.MultiTenanted()
109+
.Identity(x => x.BarId)
110+
.ForeignKey<Foo>(x => x.FooId);
111+
});
112+
113+
var mapping = store.Options.Storage.MappingFor(typeof(Bar));
114+
new DocumentTable(mapping).ForeignKeys.Single().ColumnNames.ShouldContain(TenantIdColumn.Name);
115+
}
116+
117+
[Fact]
118+
public void should_not_include_tenant_id_between_single_tenanted_docs()
119+
{
120+
var store = StoreOptions(_ =>
121+
{
122+
_.MultiTenantedWithSingleServer(ConnectionSource.ConnectionString, c =>
123+
{
124+
c.WithTenants("tenant1").InDatabaseNamed("postgres");
125+
});
126+
127+
_.Schema.For<Foo>()
128+
.SingleTenanted()
129+
.Identity(x => x.FooId);
130+
_.Schema.For<Bar>()
131+
.SingleTenanted()
132+
.Identity(x => x.BarId)
133+
.ForeignKey<Foo>(x => x.FooId);
134+
});
135+
136+
var mapping = store.Options.Storage.MappingFor(typeof(Bar));
137+
new DocumentTable(mapping).ForeignKeys.Single().ColumnNames.ShouldNotContain(TenantIdColumn.Name);
138+
}
139+
69140
#region sample_issue-with-fk-attribute
70141
public class Issue
71142
{
@@ -138,4 +209,10 @@ public class FooExtra
138209
{
139210
public Guid FooId { get; set; }
140211
}
212+
213+
public class Bar
214+
{
215+
public Guid BarId { get; set; }
216+
public Guid FooId { get; set; }
217+
}
141218
}

src/Marten/Schema/DocumentMapping.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -971,8 +971,8 @@ internal static class ForeignKeyExtensions
971971
{
972972
public static void TryMoveTenantIdFirst(this ForeignKey foreignKey, DocumentMapping mapping)
973973
{
974-
// Guard clause, do nothing if this document is not tenanted
975-
if (mapping.TenancyStyle == TenancyStyle.Single) return;
974+
// Guard clause, do nothing if this document is not tenanted or foreign key doesn't contain tenant id
975+
if (mapping.TenancyStyle == TenancyStyle.Single || foreignKey.ColumnNames.Any(x => x != TenantIdColumn.Name)) return;
976976

977977
foreignKey.ColumnNames = new string[] { TenantIdColumn.Name }
978978
.Concat(foreignKey.ColumnNames.Where(x => x != TenantIdColumn.Name)).ToArray();

0 commit comments

Comments
 (0)