API Proposal: `HasOnDeleteSetNullProperties()` to support Postgres 15+ `ON DELETE SET NULL (column_a, column_b)` column lists
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 45/100
調査の方向性
Start by reading the Npgsql annotation provider and migrations SQL generator, especially their foreign-key constraint handling; the issue’s prototype shows the intended integration points but does not name repository file paths or tests. Check existing tests for foreign-key migration SQL and annotation handling, then add coverage for the proposed property- and column-based APIs. Done means generated migrations include the selected column list for SET NULL or SET DEFAULT.
索引モデルが issue の本文から書いたものです。
説明
Preface
PostgreSQL 15+ added support for specifying a list of columns after a ON DELETE SET NULL or ON DELETE SET DEFAULT constraint, which specifies which subset of a composite foreign key's columns to set to NULL when the principal is deleted, instead of all columns in the foreign key.
The DDL constraints documentation states:
The actions SET NULL and SET DEFAULT can take a column list to specify which columns to set. Normally, all columns of the foreign-key constraint are set; setting only a subset is useful in some special cases.
For example:
FOREIGN KEY (tenant_id, author_id) REFERENCES users ON DELETE SET NULL (author_id);
Npgsql does not yet support a built-in way to target specific SET NULL columns using the standard Fluent API.
Proposed API
An extension method for ReferenceCollectionBuilder named HasOnDeleteSetNullColumns() for specifying raw column names, and HasOnDeleteSetNullProperties() for the property names, with overloads for string array and lambda expression:
public static ReferenceCollectionBuilder HasOnDeleteSetNullColumns(
this ReferenceCollectionBuilder builder,
params string[] setNullColumnNames)
public static ReferenceCollectionBuilder HasOnDeleteSetNullProperties(
this ReferenceCollectionBuilder builder,
params string[] setNullPropertyNames)
public static ReferenceCollectionBuilder<TPrincipalEntity, TDependentEntity> HasOnDeleteSetNullProperties<TPrincipalEntity, TDependentEntity>(
this ReferenceCollectionBuilder<TPrincipalEntity, TDependentEntity> builder,
Expression<Func<TDependentEntity, object?>> setNullPropertiesExpression)
where TPrincipalEntity : class
where TDependentEntity : class
Example usage
modelBuilder.Entity<Author>()
.HasMany(a => a.Posts)
.WithOne(p => p.Author)
.HasPrincipalKey(a => new { a.TenantId, a.Id })
.HasForeignKey(p => new { p.TenantId, p.AuthorId })
.OnDelete(DeleteBehavior.SetNull)
.HasOnDeleteSetNullProperties(p => p.AuthorId);
// or .HasOnDeleteSetNullProperties("AuthorId");
// or .HasOnDeleteSetNullColumns("author_id");
Workarounds
Current workarounds include manually modifying the migrations with custom SQL to drop the initial constraint and re-add it with the column list, or to manually extend the migration generator. The latter being the path I took to prototype an implementation of this proposal, which may serve as the basis for a future PR.
This prototype implements:
- Two custom annotations:
- "CustomNpgsql:OnDeleteSetNull:PropertyNames" is the property name list
- "CustomNpgsql:OnDeleteSetNull:ColumnNames" is the actual column name list
ReferenceCollectionBuilderextension methods- Adds the annotations to the Foreign Key Constraint
- Derived
NpgsqlAnnotationProvider- Translates "CustomNpgsql:OnDeleteSetNull:PropertyNames" into concrete column names and re-adds them into the relational model as "CustomNpgsql:OnDeleteSetNull:ColumnNames"
- Yields existing "CustomNpgsql:OnDeleteSetNull:ColumnNames" to the relational model
- Derived
MigrationsSqlGenerator- Adds on the additional column list SQL during ForeignKeyConstraint generation
Extension methods
public static ReferenceCollectionBuilder HasOnDeleteSetNullColumns(
this ReferenceCollectionBuilder builder,
params string[] setNullColumnNames)
{
builder.HasAnnotation(
"CustomNpgsql:OnDeleteSetNull:ColumnNames",
setNullColumnNames);
return builder;
}
public static ReferenceCollectionBuilder HasOnDeleteSetNullProperties(
this ReferenceCollectionBuilder builder,
params string[] setNullPropertyNames)
{
builder.Metadata.SetAnnotation(
"CustomNpgsql:OnDeleteSetNull:PropertyNames",
setNullPropertyNames);
return builder;
}
public static ReferenceCollectionBuilder<TPrincipalEntity, TDependentEntity> HasOnDeleteSetNullProperties<TPrincipalEntity, TDependentEntity>(
this ReferenceCollectionBuilder<TPrincipalEntity, TDependentEntity> builder,
Expression<Func<TDependentEntity, object?>> setNullPropertiesExpression)
where TPrincipalEntity : class
where TDependentEntity : class
{
string[] setNullPropertyNames = setNullPropertiesExpression
.GetMemberAccessList()
.Select(member => member.Name)
.ToArray();
builder.Metadata.SetAnnotation(
"CustomNpgsql:OnDeleteSetNull:PropertyNames",
setNullPropertyNames);
return builder;
}
Custom NpgsqlAnnotationProvider
public class CustomNpgsqlAnnotationProvider : NpgsqlAnnotationProvider
{
public CustomNpgsqlAnnotationProvider(RelationalAnnotationProviderDependencies dependencies)
: base(dependencies)
{
}
public override IEnumerable<IAnnotation> For(IForeignKeyConstraint foreignKeyConstraint, bool designTime)
{
foreach(IAnnotation baseAnnotion in base.For(foreignKeyConstraint, designTime)) {
yield return baseAnnotion;
}
foreach (IForeignKey thisForeignKey in foreignKeyConstraint.MappedForeignKeys)
{
if (thisForeignKey.FindAnnotation("CustomNpgsql:OnDeleteSetNull:ColumnNames")?.Value is string[] columnNames)
{
yield return new Annotation(
"CustomNpgsql:OnDeleteSetNull:ColumnNames",
columnNames
);
}
// Translate CustomNpgsql:OnDeleteSetNull:PropertyNames -> CustomNpgsql:OnDeleteSetNull:ColumnNames
if (thisForeignKey.FindAnnotation("CustomNpgsql:OnDeleteSetNull:PropertyNames")?.Value is string[] propertyNames)
{
// Translate property names to column names using the set of columns from the foreign key constraint
string[] columnNames = propertyNames
.SelectMany(p =>
foreignKeyConstraint.Columns
.Where(c => c.PropertyMappings.Any(pm => pm.Property.Name == p))
)
.Select(c => c.Name)
.ToArray();
yield return new Annotation(
"CustomNpgsql:OnDeleteSetNull:ColumnNames",
columnNames
);
}
}
}
}
Custom NpgsqlMigrationsSqlGenerator
public class CustomNpgsqlMigrationsSqlGenerator : NpgsqlMigrationsSqlGenerator {
protected override void ForeignKeyConstraint(AddForeignKeyOperation operation, IModel? model, MigrationCommandListBuilder builder)
{
// Write the constraint as standard. This is not yet terminated.
base.ForeignKeyConstraint(operation, model, builder);
// Only append column list to SET NULL or SET DEFAULT
if (operation.OnDelete is ReferentialAction.SetNull or ReferentialAction.SetDefault
&& operation?.FindAnnotation("CustomNpgsql:OnDeleteSetNull:ColumnNames")?.Value is string[] columnNames && columnNames.Length > 0)
{
// Append the column list
builder.Append(" (");
builder.Append(ColumnList(columnNames));
builder.Append(")");
}
}
}
- 主要言語
- C#
- スター
- 1.8k
- フォーク
- 268
- 平均マージ
- 2時間 35分
- マージ済み PR(30日)
- 136
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
npgsql/efcore.pg のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
NTS: the M ordinate is dropped from geometry SQL literals and from geometries stored in JSON対応中かも @Ahmed7991 が 5 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
npgsql/efcore.pg#4093 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
`TimeSpan`/`Duration` component members: negative values are floored, and intervals computed in SQL are read field by field対応中かも @jeremy-morren が 11 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 1 日以内に返信
-
Numeric storage parameters are formatted with the current culture, so migrations emit 0,01 instead of 0.01対応中かも @Sorbitol が 12 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 30/100
メンテナーはふだん 1 日以内に返信
-
`Contains` on a JSON primitive collection compares JSON text, so `DateTime` and `TimeOnly` items never match対応中かも @Mr444-dev が 7 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
npgsql/efcore.pg#4099 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
npgsql/efcore.pg の issue をすべて見る
似ている issue
-
Issue-Bug Needs-Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
JiaFeiMiao-K-Cat/DiscImageStudio#281 ·
メンテナーはふだん 1 日以内に返信
-
area/access-management Frontend onboarding candidate :handshake:
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
Altinn/altinn-auth#4941 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
aws/aws-sdk-net#4586 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
Kryptos-FR/MarkView.Avalonia#269 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信