diff --git a/doc/TODO.txt b/doc/TODO.txt index 1c33ce0..3a68a6c 100644 --- a/doc/TODO.txt +++ b/doc/TODO.txt @@ -8,3 +8,6 @@ TODO: auto-timestamps TODO: modified-timestamps - set updated_at and created_at in SaveXYZ - soft delete option + +TODO: generator-foreign-keys +- add auto-foreign-key checking blocks to SaveXyz diff --git a/pkg/db/errors.go b/pkg/db/errors.go index 7e982b4..ae70854 100644 --- a/pkg/db/errors.go +++ b/pkg/db/errors.go @@ -15,7 +15,7 @@ var ( ) type ForeignKey interface { - ~uint64 | ~string + ~int } type ForeignKeyError[T ForeignKey] struct { @@ -65,6 +65,18 @@ func IsSqliteUniqError(err error) bool { return errors.Is(sqliteErr.ExtendedCode, sqlite3.ErrConstraintUnique) } +// IsSqlitePrimaryKeyError checks whether an error is a SQLite `primary key` constraint violation. +// +// TODO: it's kind of annoying that SQLite returns this instead of whether the violation was +// . uniqueness or null-ness. +func IsSqlitePrimaryKeyError(err error) bool { + var sqliteErr sqlite3.Error + if !errors.As(err, &sqliteErr) { + return false + } + return errors.Is(sqliteErr.ExtendedCode, sqlite3.ErrConstraintPrimaryKey) +} + // IsSqliteNotNullError checks whether an error is a SQLite not null constraint violation. func IsSqliteNotNullError(err error) bool { var sqliteErr sqlite3.Error