How to prevent error: Introducing FOREIGN KEY constraint on table may cause cycles or multiple cascade paths

How to prevent error: Introducing FOREIGN KEY constraint on table may cause cycles or multiple cascade paths

我正在尝试为以下情况创建正确的数据库结构:

我的数据库:

我级联删除的目的:

我试过这样创建 table:

现在,当我创建 tables 时,出现错误:

Introducing FOREIGN KEY constraint 'FK_SectionMembers_Sections_SectionId' on table 'SectionMembers' may cause cycles or multiple cascade paths.

我找到了这个答案:。这似乎是我的情况,但建议的解决方案无法解决我的级联删除目的。

问题:有没有办法改变 table 结构来解决我的级联删除的目的?

SQL 由 EF Core 生成:

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE TABLE [Accounts] (
          [Id] int NOT NULL IDENTITY,
          [IdentityObjectId] nvarchar(40) NOT NULL,
          [DefaultSectionName] nvarchar(max) NULL,
          [Email] nvarchar(80) NOT NULL,
          [FirstName] nvarchar(60) NULL,
          [IsBlocked] bit NOT NULL,
          [LastName] nvarchar(80) NULL,
          [Prefix] nvarchar(16) NULL,
          CONSTRAINT [PK_Accounts] PRIMARY KEY ([Id])
      );
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (6ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE TABLE [Organizations] (
          [Id] int NOT NULL IDENTITY,
          [Name] nvarchar(36) NOT NULL,
          CONSTRAINT [PK_Organizations] PRIMARY KEY ([Id])
      );
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE TABLE [OrganizationMembers] (
          [Id] int NOT NULL IDENTITY,
          [AccountId] int NOT NULL,
          [IsBlocked] bit NOT NULL,
          [IsNew] bit NOT NULL,
          [OrganizationId] int NOT NULL,
          [UserName] nvarchar(24) NOT NULL,
          CONSTRAINT [PK_OrganizationMembers] PRIMARY KEY ([Id]),
          CONSTRAINT [FK_OrganizationMembers_Accounts_AccountId] FOREIGN KEY ([AccountId]) REFERENCES [Accounts] ([Id]) ON DELETE CASCADE,
          CONSTRAINT [FK_OrganizationMembers_Organizations_OrganizationId] FOREIGN KEY ([OrganizationId]) REFERENCES [Organizations] ([Id]) ON DELETE CASCADE
      );
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE TABLE [Sections] (
          [Id] int NOT NULL IDENTITY,
          [IsLocked] bit NOT NULL,
          [Name] nvarchar(48) NOT NULL,
          [OrganizationId] int NOT NULL,
          CONSTRAINT [PK_Sections] PRIMARY KEY ([Id]),
          CONSTRAINT [FK_Sections_Organizations_OrganizationId] FOREIGN KEY ([OrganizationId]) REFERENCES [Organizations] ([Id]) ON DELETE CASCADE
      );
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (35ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE TABLE [SectionMembers] (
          [Id] int NOT NULL IDENTITY,
          [AccountLevel] tinyint NOT NULL,
          [IsBlocked] bit NOT NULL,
          [IsNew] bit NOT NULL,
          [OrganizationMemberId] int NOT NULL,
          [SectionId] int NOT NULL,
          CONSTRAINT [PK_SectionMembers] PRIMARY KEY ([Id]),
          CONSTRAINT [FK_SectionMembers_OrganizationMembers_OrganizationMemberId] FOREIGN KEY ([OrganizationMemberId]) REFERENCES [OrganizationMembers] ([Id]) ON DELETE CASCADE,
          CONSTRAINT [FK_SectionMembers_Sections_SectionId] FOREIGN KEY ([SectionId]) REFERENCES [Sections] ([Id]) ON DELETE CASCADE
      );

Error: Introducing FOREIGN KEY constraint 'FK_SectionMembers_Sections_SectionId' on table 'SectionMembers' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
  • Has multiple Accounts
  • An Account can be member of multiple Organizations => OrganizationMember
  • An Organization has multiple Sections
  • An OrganizationMember can be member of multiple Sections => SectionMember

这个布局怎么样

  • 有 N 个帐户 => Account (PK account_id)
  • 有 N 个组织 => Organization (PK organization_id)
  • 一个组织有 1..N 个部分 => Section (PK section_id, FK organization_id NOT NULL)
  • 一个帐户可以是多个部分的成员=> AccountSection (PK (FK account_id, FK section_id))

最后,该帐户始终是部分的成员,并且它只是隐含地是组织的成员(是否可以通过简单的 JOIN 来确定).