Skip to content

Commit a60d27c

Browse files
Deferrable foreign keys (#39443)
Fixes #11903
1 parent 39c0ef8 commit a60d27c

37 files changed

Lines changed: 1062 additions & 28 deletions

File tree

‎src/EFCore.Design/Migrations/Design/CSharpMigrationOperationGenerator.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,14 @@ protected virtual void Generate(AddForeignKeyOperation operation, IndentedString
295295
.Append(Code.Literal(operation.OnDelete));
296296
}
297297

298+
if (operation.Deferrability != ConstraintDeferrability.NotDeferrable)
299+
{
300+
builder
301+
.AppendLine(",")
302+
.Append("deferrability: ")
303+
.Append(Code.Literal(operation.Deferrability));
304+
}
305+
298306
builder.Append(")");
299307

300308
Annotations(operation.GetAnnotations(), builder);
@@ -1308,6 +1316,14 @@ protected virtual void Generate(CreateTableOperation operation, IndentedStringBu
13081316
.Append(Code.Literal(foreignKey.OnDelete));
13091317
}
13101318

1319+
if (foreignKey.Deferrability != ConstraintDeferrability.NotDeferrable)
1320+
{
1321+
builder
1322+
.AppendLine(",")
1323+
.Append("deferrability: ")
1324+
.Append(Code.Literal(foreignKey.Deferrability));
1325+
}
1326+
13111327
builder.Append(")");
13121328

13131329
Annotations(foreignKey.GetAnnotations(), builder);

‎src/EFCore.Design/Migrations/Design/MigrationsCodeGenerator.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ protected virtual IEnumerable<string> GetNamespaces(IEnumerable<MigrationOperati
9898
.Concat(operations.OfType<UpdateDataOperation>().SelectMany(o => new[] { o.KeyValues, o.Values }))
9999
.Concat(operations.OfType<DeleteDataOperation>().Select(o => o.KeyValues))
100100
.SelectMany(GetDataNamespaces))
101+
.Concat(
102+
operations.OfType<AddForeignKeyOperation>()
103+
.Concat(operations.OfType<CreateTableOperation>().SelectMany(o => o.ForeignKeys))
104+
.Any(o => o.Deferrability != ConstraintDeferrability.NotDeferrable)
105+
? [typeof(ConstraintDeferrability).Namespace!]
106+
: [])
101107
.Concat(GetAnnotationNamespaces(GetAnnotatables(operations)));
102108

103109
private static IEnumerable<string> GetColumnNamespaces(ColumnOperation columnOperation)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore;
5+
6+
/// <summary>
7+
/// Indicates whether checking of a database constraint can be deferred until the end of the transaction,
8+
/// as defined by the SQL standard (<c>[NOT] DEFERRABLE INITIALLY {IMMEDIATE | DEFERRED}</c>).
9+
/// </summary>
10+
/// <remarks>
11+
/// <para>
12+
/// Not all databases support deferrable constraints; for example, PostgreSQL, Oracle and SQLite (foreign keys only)
13+
/// support them, while SQL Server and MySQL do not.
14+
/// </para>
15+
/// <para>
16+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and
17+
/// examples.
18+
/// </para>
19+
/// </remarks>
20+
public enum ConstraintDeferrability
21+
{
22+
/// <summary>
23+
/// The constraint is checked immediately after every statement, and checking cannot be deferred (<c>NOT DEFERRABLE</c>).
24+
/// This is the default.
25+
/// </summary>
26+
NotDeferrable = 0,
27+
28+
/// <summary>
29+
/// The constraint is checked immediately after every statement by default, but checking can be deferred to the
30+
/// end of the transaction (e.g. with <c>SET CONSTRAINTS ... DEFERRED</c>) (<c>DEFERRABLE INITIALLY IMMEDIATE</c>).
31+
/// </summary>
32+
DeferrableInitiallyImmediate = 1,
33+
34+
/// <summary>
35+
/// The constraint is checked at the end of the transaction (<c>DEFERRABLE INITIALLY DEFERRED</c>).
36+
/// </summary>
37+
DeferrableInitiallyDeferred = 2
38+
}

‎src/EFCore.Relational/Design/AnnotationCodeGenerator.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,12 @@ public virtual IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
435435
nameof(RelationalForeignKeyBuilderExtensions.ExcludeForeignKeyFromMigrations),
436436
methodCallCodeFragments);
437437

438+
GenerateSimpleFluentApiCall(
439+
annotations,
440+
RelationalAnnotationNames.Deferrability,
441+
nameof(RelationalForeignKeyBuilderExtensions.HasDeferrability),
442+
methodCallCodeFragments);
443+
438444
methodCallCodeFragments.AddRange(GenerateFluentApiCallsHelper(foreignKey, annotations, GenerateFluentApi));
439445

440446
return methodCallCodeFragments;

‎src/EFCore.Relational/Design/Internal/RelationalCSharpRuntimeAnnotationCodeGenerator.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,6 +2467,7 @@ public override void Generate(IForeignKey foreignKey, CSharpRuntimeAnnotationCod
24672467
else
24682468
{
24692469
parameters.Annotations.Remove(RelationalAnnotationNames.IsForeignKeyExcludedFromMigrations);
2470+
parameters.Annotations.Remove(RelationalAnnotationNames.Deferrability);
24702471
}
24712472

24722473
base.Generate(foreignKey, parameters);

0 commit comments

Comments
 (0)