Critical bug fix for tenant registration failure caused by incorrect EF Core migration.
## Problem
The AddUserTenantRoles migration generated duplicate columns:
- Application columns: user_id, tenant_id (used by code)
- Shadow FK columns: user_id1, tenant_id1 (incorrect EF Core generation)
Foreign key constraints referenced wrong columns (user_id1/tenant_id1), causing all
tenant registrations to fail with:
```
violates foreign key constraint "FK_user_tenant_roles_tenants_tenant_id1"
```
## Root Cause
UserTenantRoleConfiguration.cs used string column names in HasForeignKey(),
combined with Value Object properties (UserId/TenantId), causing EF Core to
create shadow properties with duplicate names (user_id1, tenant_id1).
## Solution
1. **Configuration Change**:
- Keep Value Object properties (UserId, TenantId) for application use
- Ignore navigation properties (User, Tenant) to prevent shadow property generation
- Let EF Core use the converted Value Object columns for data storage
2. **Migration Change**:
- Delete incorrect AddUserTenantRoles migration
- Generate new FixUserTenantRolesIgnoreNavigation migration
- Drop duplicate columns (user_id1, tenant_id1)
- Recreate FK constraints referencing correct columns (user_id, tenant_id)
## Changes
- Modified: UserTenantRoleConfiguration.cs
- Ignore navigation properties (User, Tenant)
- Use Value Object conversion for UserId/TenantId columns
- Deleted: 20251103135644_AddUserTenantRoles migration (broken)
- Added: 20251103150353_FixUserTenantRolesIgnoreNavigation migration (fixed)
- Updated: IdentityDbContextModelSnapshot.cs (no duplicate columns)
- Added: test-bugfix.ps1 (regression test script)
## Test Results
- Tenant registration: SUCCESS
- JWT Token generation: SUCCESS
- Refresh Token generation: SUCCESS
- Foreign key constraints: CORRECT (user_id, tenant_id)
## Database Schema (After Fix)
```sql
CREATE TABLE identity.user_tenant_roles (
id uuid PRIMARY KEY,
user_id uuid NOT NULL, -- Used by application & FK
tenant_id uuid NOT NULL, -- Used by application & FK
role varchar(50) NOT NULL,
assigned_at timestamptz NOT NULL,
assigned_by_user_id uuid,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);
```
Fixes: BUG-002 (CRITICAL)
Severity: CRITICAL - Blocked all tenant registrations
Impact: Day 5 RBAC feature now working
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
328 lines
13 KiB
C#
328 lines
13 KiB
C#
// <auto-generated />
|
|
using System;
|
|
using ColaFlow.Modules.Identity.Infrastructure.Persistence;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
|
|
|
#nullable disable
|
|
|
|
namespace ColaFlow.Modules.Identity.Infrastructure.Persistence.Migrations
|
|
{
|
|
[DbContext(typeof(IdentityDbContext))]
|
|
partial class IdentityDbContextModelSnapshot : ModelSnapshot
|
|
{
|
|
protected override void BuildModel(ModelBuilder modelBuilder)
|
|
{
|
|
#pragma warning disable 612, 618
|
|
modelBuilder
|
|
.HasAnnotation("ProductVersion", "9.0.10")
|
|
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
|
|
|
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
|
|
|
modelBuilder.Entity("ColaFlow.Modules.Identity.Domain.Aggregates.Tenants.Tenant", b =>
|
|
{
|
|
b.Property<Guid>("Id")
|
|
.ValueGeneratedOnAdd()
|
|
.HasColumnType("uuid")
|
|
.HasColumnName("id");
|
|
|
|
b.Property<DateTime>("CreatedAt")
|
|
.HasColumnType("timestamp with time zone")
|
|
.HasColumnName("created_at");
|
|
|
|
b.Property<int>("MaxProjects")
|
|
.HasColumnType("integer")
|
|
.HasColumnName("max_projects");
|
|
|
|
b.Property<int>("MaxStorageGB")
|
|
.HasColumnType("integer")
|
|
.HasColumnName("max_storage_gb");
|
|
|
|
b.Property<int>("MaxUsers")
|
|
.HasColumnType("integer")
|
|
.HasColumnName("max_users");
|
|
|
|
b.Property<string>("Name")
|
|
.IsRequired()
|
|
.HasMaxLength(100)
|
|
.HasColumnType("character varying(100)")
|
|
.HasColumnName("name");
|
|
|
|
b.Property<string>("Plan")
|
|
.IsRequired()
|
|
.HasMaxLength(50)
|
|
.HasColumnType("character varying(50)")
|
|
.HasColumnName("plan");
|
|
|
|
b.Property<string>("Slug")
|
|
.IsRequired()
|
|
.HasMaxLength(50)
|
|
.HasColumnType("character varying(50)")
|
|
.HasColumnName("slug");
|
|
|
|
b.Property<string>("SsoConfig")
|
|
.HasColumnType("jsonb")
|
|
.HasColumnName("sso_config");
|
|
|
|
b.Property<string>("Status")
|
|
.IsRequired()
|
|
.HasMaxLength(50)
|
|
.HasColumnType("character varying(50)")
|
|
.HasColumnName("status");
|
|
|
|
b.Property<DateTime?>("SuspendedAt")
|
|
.HasColumnType("timestamp with time zone")
|
|
.HasColumnName("suspended_at");
|
|
|
|
b.Property<string>("SuspensionReason")
|
|
.HasMaxLength(500)
|
|
.HasColumnType("character varying(500)")
|
|
.HasColumnName("suspension_reason");
|
|
|
|
b.Property<DateTime?>("UpdatedAt")
|
|
.HasColumnType("timestamp with time zone")
|
|
.HasColumnName("updated_at");
|
|
|
|
b.HasKey("Id");
|
|
|
|
b.HasIndex("Slug")
|
|
.IsUnique()
|
|
.HasDatabaseName("ix_tenants_slug");
|
|
|
|
b.ToTable("tenants", (string)null);
|
|
});
|
|
|
|
modelBuilder.Entity("ColaFlow.Modules.Identity.Domain.Aggregates.Users.RefreshToken", b =>
|
|
{
|
|
b.Property<Guid>("Id")
|
|
.ValueGeneratedOnAdd()
|
|
.HasColumnType("uuid");
|
|
|
|
b.Property<DateTime>("CreatedAt")
|
|
.HasColumnType("timestamp with time zone")
|
|
.HasColumnName("created_at");
|
|
|
|
b.Property<string>("DeviceInfo")
|
|
.HasMaxLength(500)
|
|
.HasColumnType("character varying(500)")
|
|
.HasColumnName("device_info");
|
|
|
|
b.Property<DateTime>("ExpiresAt")
|
|
.HasColumnType("timestamp with time zone")
|
|
.HasColumnName("expires_at");
|
|
|
|
b.Property<string>("IpAddress")
|
|
.HasMaxLength(50)
|
|
.HasColumnType("character varying(50)")
|
|
.HasColumnName("ip_address");
|
|
|
|
b.Property<string>("ReplacedByToken")
|
|
.HasMaxLength(500)
|
|
.HasColumnType("character varying(500)")
|
|
.HasColumnName("replaced_by_token");
|
|
|
|
b.Property<DateTime?>("RevokedAt")
|
|
.HasColumnType("timestamp with time zone")
|
|
.HasColumnName("revoked_at");
|
|
|
|
b.Property<string>("RevokedReason")
|
|
.HasMaxLength(500)
|
|
.HasColumnType("character varying(500)")
|
|
.HasColumnName("revoked_reason");
|
|
|
|
b.Property<Guid>("TenantId")
|
|
.HasColumnType("uuid")
|
|
.HasColumnName("tenant_id");
|
|
|
|
b.Property<string>("TokenHash")
|
|
.IsRequired()
|
|
.HasMaxLength(500)
|
|
.HasColumnType("character varying(500)")
|
|
.HasColumnName("token_hash");
|
|
|
|
b.Property<string>("UserAgent")
|
|
.HasMaxLength(500)
|
|
.HasColumnType("character varying(500)")
|
|
.HasColumnName("user_agent");
|
|
|
|
b.Property<Guid>("UserId")
|
|
.HasColumnType("uuid")
|
|
.HasColumnName("user_id");
|
|
|
|
b.HasKey("Id");
|
|
|
|
b.HasIndex("ExpiresAt")
|
|
.HasDatabaseName("ix_refresh_tokens_expires_at");
|
|
|
|
b.HasIndex("TenantId")
|
|
.HasDatabaseName("ix_refresh_tokens_tenant_id");
|
|
|
|
b.HasIndex("TokenHash")
|
|
.IsUnique()
|
|
.HasDatabaseName("ix_refresh_tokens_token_hash");
|
|
|
|
b.HasIndex("UserId")
|
|
.HasDatabaseName("ix_refresh_tokens_user_id");
|
|
|
|
b.ToTable("refresh_tokens", "identity");
|
|
});
|
|
|
|
modelBuilder.Entity("ColaFlow.Modules.Identity.Domain.Aggregates.Users.User", b =>
|
|
{
|
|
b.Property<Guid>("Id")
|
|
.ValueGeneratedOnAdd()
|
|
.HasColumnType("uuid")
|
|
.HasColumnName("id");
|
|
|
|
b.Property<string>("AuthProvider")
|
|
.IsRequired()
|
|
.HasMaxLength(50)
|
|
.HasColumnType("character varying(50)")
|
|
.HasColumnName("auth_provider");
|
|
|
|
b.Property<string>("AvatarUrl")
|
|
.HasMaxLength(500)
|
|
.HasColumnType("character varying(500)")
|
|
.HasColumnName("avatar_url");
|
|
|
|
b.Property<DateTime>("CreatedAt")
|
|
.HasColumnType("timestamp with time zone")
|
|
.HasColumnName("created_at");
|
|
|
|
b.Property<string>("Email")
|
|
.IsRequired()
|
|
.HasMaxLength(255)
|
|
.HasColumnType("character varying(255)")
|
|
.HasColumnName("email");
|
|
|
|
b.Property<string>("EmailVerificationToken")
|
|
.HasMaxLength(255)
|
|
.HasColumnType("character varying(255)")
|
|
.HasColumnName("email_verification_token");
|
|
|
|
b.Property<DateTime?>("EmailVerifiedAt")
|
|
.HasColumnType("timestamp with time zone")
|
|
.HasColumnName("email_verified_at");
|
|
|
|
b.Property<string>("ExternalEmail")
|
|
.HasMaxLength(255)
|
|
.HasColumnType("character varying(255)")
|
|
.HasColumnName("external_email");
|
|
|
|
b.Property<string>("ExternalUserId")
|
|
.HasMaxLength(255)
|
|
.HasColumnType("character varying(255)")
|
|
.HasColumnName("external_user_id");
|
|
|
|
b.Property<string>("FullName")
|
|
.IsRequired()
|
|
.HasMaxLength(100)
|
|
.HasColumnType("character varying(100)")
|
|
.HasColumnName("full_name");
|
|
|
|
b.Property<string>("JobTitle")
|
|
.HasMaxLength(100)
|
|
.HasColumnType("character varying(100)")
|
|
.HasColumnName("job_title");
|
|
|
|
b.Property<DateTime?>("LastLoginAt")
|
|
.HasColumnType("timestamp with time zone")
|
|
.HasColumnName("last_login_at");
|
|
|
|
b.Property<string>("PasswordHash")
|
|
.IsRequired()
|
|
.HasMaxLength(255)
|
|
.HasColumnType("character varying(255)")
|
|
.HasColumnName("password_hash");
|
|
|
|
b.Property<string>("PasswordResetToken")
|
|
.HasMaxLength(255)
|
|
.HasColumnType("character varying(255)")
|
|
.HasColumnName("password_reset_token");
|
|
|
|
b.Property<DateTime?>("PasswordResetTokenExpiresAt")
|
|
.HasColumnType("timestamp with time zone")
|
|
.HasColumnName("password_reset_token_expires_at");
|
|
|
|
b.Property<string>("PhoneNumber")
|
|
.HasMaxLength(50)
|
|
.HasColumnType("character varying(50)")
|
|
.HasColumnName("phone_number");
|
|
|
|
b.Property<string>("Status")
|
|
.IsRequired()
|
|
.HasMaxLength(50)
|
|
.HasColumnType("character varying(50)")
|
|
.HasColumnName("status");
|
|
|
|
b.Property<Guid>("TenantId")
|
|
.HasColumnType("uuid")
|
|
.HasColumnName("tenant_id");
|
|
|
|
b.Property<DateTime?>("UpdatedAt")
|
|
.HasColumnType("timestamp with time zone")
|
|
.HasColumnName("updated_at");
|
|
|
|
b.HasKey("Id");
|
|
|
|
b.HasIndex("TenantId", "Email")
|
|
.IsUnique()
|
|
.HasDatabaseName("ix_users_tenant_id_email");
|
|
|
|
b.ToTable("users", (string)null);
|
|
});
|
|
|
|
modelBuilder.Entity("ColaFlow.Modules.Identity.Domain.Aggregates.Users.UserTenantRole", b =>
|
|
{
|
|
b.Property<Guid>("Id")
|
|
.ValueGeneratedOnAdd()
|
|
.HasColumnType("uuid")
|
|
.HasColumnName("id");
|
|
|
|
b.Property<DateTime>("AssignedAt")
|
|
.HasColumnType("timestamp with time zone")
|
|
.HasColumnName("assigned_at");
|
|
|
|
b.Property<Guid?>("AssignedByUserId")
|
|
.HasColumnType("uuid")
|
|
.HasColumnName("assigned_by_user_id");
|
|
|
|
b.Property<string>("Role")
|
|
.IsRequired()
|
|
.HasMaxLength(50)
|
|
.HasColumnType("character varying(50)")
|
|
.HasColumnName("role");
|
|
|
|
b.Property<Guid>("TenantId")
|
|
.HasColumnType("uuid")
|
|
.HasColumnName("tenant_id");
|
|
|
|
b.Property<Guid>("UserId")
|
|
.HasColumnType("uuid")
|
|
.HasColumnName("user_id");
|
|
|
|
b.HasKey("Id");
|
|
|
|
b.HasIndex("Role")
|
|
.HasDatabaseName("ix_user_tenant_roles_role");
|
|
|
|
b.HasIndex("TenantId")
|
|
.HasDatabaseName("ix_user_tenant_roles_tenant_id");
|
|
|
|
b.HasIndex("UserId")
|
|
.HasDatabaseName("ix_user_tenant_roles_user_id");
|
|
|
|
b.HasIndex("UserId", "TenantId")
|
|
.IsUnique()
|
|
.HasDatabaseName("uq_user_tenant_roles_user_tenant");
|
|
|
|
b.ToTable("user_tenant_roles", "identity");
|
|
});
|
|
#pragma warning restore 612, 618
|
|
}
|
|
}
|
|
}
|