Path: redb/redb.Core/Exceptions/RedbUniqueViolationException.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Exceptions/RedbUniqueViolationException.cs
----
using System;
namespace redb.Core.Exceptions;
///
/// A save was rejected because a [RedbUnique] key is already taken by another object of the
/// scheme. Raised in place of the driver's own unique-violation error (PostgreSQL 23505, SQL Server
/// 2601/2627, SQLite 2067) so that callers can catch one type on every provider and read which key
/// collided, rather than match on message text.
///
///
/// Everything but is best-effort: the driver message names the index and, on
/// PostgreSQL and SQL Server, the key tuple — from which the structure and therefore the scheme and
/// property are resolved. SQLite names only the columns. When something could not be resolved the
/// property is null; the original exception is always attached.
///
///
public class RedbUniqueViolationException : Exception
{
/// Scheme of the object that was being saved, when resolved.
public long? SchemeId { get; }
/// Scheme name, when resolved.
public string? SchemeName { get; }
/// Structure (field) whose key collided, when the driver reported the key tuple.
public long? StructureId { get; }
/// Property backing that structure, when resolved.
public string? PropertyName { get; }
/// Name of the unique index or constraint the database reported, when it did.
public string? ConstraintName { get; }
/// The driver's own detail line (PostgreSQL DETAIL, SQL Server message), verbatim.
public string? Detail { get; }
/// Which unique index rejected the save - the object key (ValueUnique) or a
/// [RedbUnique] property (BR-8, 2026-09-02). Classified on every provider: PostgreSQL
/// and MSSQL name the index, SQLite names the columns; both spellings resolve.
public RedbUniqueViolationKind Kind { get; }
/// The driver exception.
public Exception Cause { get; }
public RedbUniqueViolationException(
long? schemeId,
string? schemeName,
long? structureId,
string? propertyName,
string? constraintName,
string? detail,
Exception cause,
RedbUniqueViolationKind kind = RedbUniqueViolationKind.Unknown)
: base(BuildMessage(schemeId, schemeName, structureId, propertyName, constraintName, detail, kind), cause)
{
SchemeId = schemeId;
SchemeName = schemeName;
StructureId = structureId;
PropertyName = propertyName;
ConstraintName = constraintName;
Detail = detail;
Cause = cause;
Kind = kind;
}
private static string BuildMessage(
long? schemeId, string? schemeName, long? structureId, string? propertyName,
string? constraintName, string? detail, RedbUniqueViolationKind kind)
{
var where = kind == RedbUniqueViolationKind.ObjectKey && propertyName == null
? "the object key (ValueUnique)" + (schemeName != null ? $" of scheme '{schemeName}'" : schemeId.HasValue ? $" of scheme id={schemeId}" : "")
: propertyName != null
? $"property '{propertyName}'" + (schemeName != null ? $" of scheme '{schemeName}'" : schemeId.HasValue ? $" of scheme id={schemeId}" : "")
: structureId.HasValue
? $"structure id={structureId}"
: constraintName != null ? $"index '{constraintName}'" : "a unique key";
var tail = string.IsNullOrWhiteSpace(detail) ? "" : $" Database says: {detail}";
return $"Unique key violated on {where}: another object of the scheme already holds this value. " +
$"Keys are compared on their canonical form (see UniqueKeyEncoder); a soft-deleted object " +
$"releases its key, a live one does not.{tail}";
}
}