ABP Framework version: v8.1.4
UI Type: Angular
Database System: EF Core ( PostgreSQL)
Tiered (for MVC) or Auth Server Separated (for Angular): yes
Exception message and full stack trace: [17:50:42 INF] fail: 11/16/2024 17:50:42.340 CoreEventId.SaveChangesFailed[10000] (Microsoft.EntityFrameworkCore.Update) An exception occurred in the database while saving changes for context type 'Doohlink.PlaylistManagement.EntityFrameworkCore.PlaylistManagementDbContext'. Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details. ---> Npgsql.PostgresException (0x80004005): 23503: insert or update on table "PlaylistManagementPlaylistFiles" violates foreign key constraint "FK_PlaylistManagementPlaylistFiles_PlaylistManagementFiles_Fil~"
DETAIL: Key (FileId)=(3a1647cd-ead1-7bbc-8095-7c274e70176b) is not present in table "PlaylistManagementFiles".
Steps to reproduce the issue: Hello, I have a general question about data filtering. I have 3 tables in my database Playlist,File and PlaylistFile. you can see the entities and aggregate roots below.
File
public class File : AggregateRoot<Guid>, IMultiTenant, ISoftDelete
{
public bool IsDeleted { get; protected set; }
public Guid? TenantId { get; protected set; }
public string Name { get; private set; }= null!;
//rest of the code
}
Playlist
public class Playlist : FullAuditedAggregateRoot<Guid>, IMultiTenant
{
public Guid? TenantId { get; protected set; }
public string Name { get; private set; } = null!;
public string? Description { get; set; }
//rest of the code
}
PlaylistFile
public class PlaylistFile:CreationAuditedAggregateRoot<Guid>,ISoftDelete
{
public bool IsDeleted { get; protected set; }
public Guid PlaylistId { get; private set; }
public Guid FileId { get; private set; }
//rest of the code
}
so as you can see playlist and file is having imultitenant interface but since playlistfile belongs to two other table, i preferred to not make it imultitenat. The problem i am having is when i use separete db for a tenant. since PlaylistFile doesn't have imultitenant interface, when i use separate db for tenant even if the current tenant is the tenant with separate db connection string, it tries to insert it to the shared database. Here is an example from an app service.
public async Task CreateBatchAsync(Guid playlistId, CreateOrUpdatePlaylistFilesDto input)
{
await _playlistRepository.GetAsync(playlistId);
var newFileIds = input.FileIds.ToList();
var playlistFiles = await _playlistFileManager.CreateBatchAsync(playlistId, newFileIds);
foreach (var playlistFile in playlistFiles)
{
await _playlistFileRepository.InsertAsync(playlistFile);
}
}
in this code _playlistRepository is looking at the separate db since it has imultitenant interface but when i try to insert the records through _playlistFileRepository it takes the shared connection string. And i am getting exception since no fileid is present on the shared db. Is this common behavior? I know that i can give imultitenant interface to PlaylistFile aggregate root, but i do not prefer that since it is a table that will reflect the tenantid from their parent table. Is there any other way to fix it?
8 Answer(s)
-
0
Hi,
Because they have foreign key relationships, they must be in the same database.
you can two ways:
- Add
IMultiTenant
interface to other entities - Remove database foreign key constraint, check-in code
- Add
-
0
Hello again, Maybe it is little unclear what i want to say. As an example If Tenant A has a separate db connection string and table does not have any imultitenant interface and if you do the operation on behalf of that current tenant, shouldn't it look at the Tenant A database first? That's what i am trying to achieve. So in my application,
I have shared database which has connection string.
Host=localhost;Port=5432;Database=Adzup;
and "Tenant A" has connection string
Host=localhost;Port=5432;Database=Adzup_TenantA;
when i apply my migrations... Playlist, File and PlaylistFile tables are created for both databases. and when i do the insert
public async Task CreateBatchAsync(Guid playlistId, CreateOrUpdatePlaylistFilesDto input) { await _playlistRepository.GetAsync(playlistId); var newFileIds = input.FileIds.ToList(); var playlistFiles = await _playlistFileManager.CreateBatchAsync(playlistId, newFileIds); foreach (var playlistFile in playlistFiles) { await _playlistFileRepository.InsertAsync(playlistFile); } }
i am expecting playlistfiles should be inserted with the connection string
Host=localhost;Port=5432;Database=Adzup_TenantA;
since my CurrentTenant has this connection string. but instead it is trying to insert it withHost=localhost;Port=5432;Database=Adzup;
i can see that foreign key that is mentioned in the error is inside the dbHost=localhost;Port=5432;Database=Adzup_TenantA;
-
0
Hi,
You must add
IMultiTenant
interface to a multi-tenancy entity, this is the convention of the framework. -
0
Ok i see, It seems tables created on Separate database without imultitenant interface is never used.
Then can we come to the conclusion..
if you are building a multitenant system and if you want to use separate db connection strings, even if it is not necessary to have tenantid in tables since you use separate db connection strings, you should use it in abp.
-
0
Hi,
It seems tables created on Separate database without imultitenant interface is never used.
you can check the current context if it is host to create table like this:
if you are building a multitenant system and if you want to use separate db connection strings, even if it is not necessary to have tenantid in tables since you use separate db connection strings, you should use it in abp.
No tenantid means it's a table can shared between host and tenant
-
0
Hello @liangshiwei, I am kind of lost over here, i know that i am asking too many questions but i want to understand truly so i can build it up according to that. thank you for your patience with me :)
As i know tenantid is useful when you have single database and have multi tenants in that single database. But what i am trying to build is to create separate databases for each tenant. Then i believe i don't need any TenantId since it is encapsulated inside one database for each tenant. But as i see it in my code when the aggregate root doesn't have any imultitenant interface even the tenant has separate connection string, it looks for the shared database.
So i come to conclusion that in that separate database even if i do not need TenantId, i should have TenantId so the abp can do the operation on Separate Database?
Did i get it right? or am i missing sth over here?
-
0
Hi,
Yes, the
IMultiTenant
interface is always needed for multi-tenant entities.if there is no
IMultiTenant
, ABP will use it as a host entity -
0
Okay i have changed all my aggregate roots to implement imultitenant interface. Thank you for the explanation.