Skip to content

[BUG] - ERROR: insert or update on table "UserPreferences" violates foreign key #90

@davidroler

Description

@davidroler

I've installed ControlR in my lab few days ago to try it out and eventually replace Remotely.

All tests were working, I was able to:

  1. Add user
  2. Register devices
  3. Remote access

When this morning I noticed that all configuration I've made is gone. I'm still able to login to the portal, but I can't see any customazation we've made, and clients can't connect to anymore.

Checking the DB logs I've found somes errors.

Server Info (if self-hosting)

Server OS:
Language/Locale of Server OS: K8S/Ubuntu 22.04
Docker image tag: 0.19.2.0

Other relevant info:

Logs

[POSTGRES]
│ 2026-02-05 20:18:49.600 UTC [33] LOG: checkpoint starting: time │
│ 2026-02-05 20:18:50.181 UTC [33] LOG: checkpoint complete: wrote 4 buffers (0.0%), wrote 1 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; w │
│ 2026-02-05 20:23:49.280 UTC [33] LOG: checkpoint starting: time │
│ 2026-02-05 20:23:49.831 UTC [33] LOG: checkpoint complete: wrote 5 buffers (0.0%), wrote 1 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; w │
│ 2026-02-05 20:26:21.567 UTC [2650] ERROR: insert or update on table "UserPreferences" violates foreign key constraint "FK_UserPreferences_Tenants_Tena │
│ 2026-02-05 20:26:21.567 UTC [2650] DETAIL: Key (TenantId)=(652e1de0-41b1-4d8a-9054-587c2c8cce69) is not present in table "Tenants". │
│ 2026-02-05 20:26:21.567 UTC [2650] STATEMENT: INSERT INTO "UserPreferences" ("Name", "TenantId", "UserId", "Value") │
│ VALUES ($1, $2, $3, $4) │
│ RETURNING "Id", "CreatedAt" │
│ 2026-02-05 20:26:27.315 UTC [2658] ERROR: insert or update on table "UserPreferences" violates foreign key constraint "FK_UserPreferences_Tenants_Tena │
│ 2026-02-05 20:26:27.315 UTC [2658] DETAIL: Key (TenantId)=(652e1de0-41b1-4d8a-9054-587c2c8cce69) is not present in table "Tenants". │
│ 2026-02-05 20:26:27.315 UTC [2658] STATEMENT: INSERT INTO "UserPreferences" ("Name", "TenantId", "UserId", "Value") │
│ VALUES ($1, $2, $3, $4) │
│ RETURNING "Id", "CreatedAt" │
│ 2026-02-05 20:28:49.932 UTC [33] LOG: checkpoint starting: time │
│ 2026-02-05 20:28:51.849 UTC [33] LOG: checkpoint complete: wrote 17 buffers (0.1%), wrote 1 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; │
│ 2026-02-05 20:33:49.952 UTC [33] LOG: checkpoint starting: time

[APP]
An exception occurred in the database while saving changes for context type 'ControlR.Web.Server.Data.AppDb'.
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 "UserPreferences" violates foreign key constraint "FK_UserPreferences_Tenants_TenantId"

DETAIL: Detail redacted as it may contain sensitive data. Specify 'Include Error Detail' in the connection string to include this information.
at Npgsql.Internal.NpgsqlConnector.ReadMessageLong(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder1.StateMachineBox1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(Boolean async, CommandBehavior behavior, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
Exception data:
Severity: ERROR
SqlState: 23503
MessageText: insert or update on table "UserPreferences" violates foreign key constraint "FK_UserPreferences_Tenants_TenantId"
Detail: Detail redacted as it may contain sensitive data. Specify 'Include Error Detail' in the connection string to include this information.
SchemaName: public
TableName: UserPreferences
ConstraintName: FK_UserPreferences_Tenants_TenantId
File: ri_triggers.c
Line: 2772
Routine: ri_ReportViolation
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChangesAsync(IList1 entries, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList1 entriesToSave, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(StateManager stateManager, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken) at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)

Log Timestamp(s)

Regards,

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions