db.Error. Use the standard errors.Is function to check for specific sentinel errors — this works correctly even when errors are wrapped.
All GORM errors are package-level
var values, not types. Always use errors.Is rather than direct equality (==) so that wrapping is handled correctly.Error reference
ErrRecordNotFound
ErrRecordNotFound
Message:
record not foundOccurs when: First, Take, or Last is called and no record matches the conditions.How to handle:Find does not return this error when the result set is empty — it returns a nil error with RowsAffected == 0.ErrInvalidTransaction
ErrInvalidTransaction
Message:
invalid transactionOccurs when: Commit or Rollback is called on a *DB that is not inside an active transaction, or when Begin cannot start a transaction because the pool does not implement TxBeginner or ConnPoolBeginner.ErrNotImplemented
ErrNotImplemented
Message:
not implementedOccurs when: A dialector or migrator method is called that the driver has not implemented.ErrMissingWhereClause
ErrMissingWhereClause
Message:
WHERE conditions requiredOccurs when: An Update, Updates, or Delete is executed without any WHERE conditions and Config.AllowGlobalUpdate is false (the default).ErrUnsupportedRelation
ErrUnsupportedRelation
Message:
unsupported relationsOccurs when: db.Association(column) is called with a field name that does not correspond to a known relationship on the model schema.ErrPrimaryKeyRequired
ErrPrimaryKeyRequired
Message:
primary key requiredOccurs when: An operation requires a primary key (e.g., FindInBatches pagination, or association operations) but the model has none defined or the value is zero.ErrModelValueRequired
ErrModelValueRequired
Message:
model value requiredOccurs when: A Model or target value is required for an operation but was not provided.ErrModelAccessibleFieldsRequired
ErrModelAccessibleFieldsRequired
Message:
model accessible fields requiredOccurs when: GORM cannot find any accessible (readable/writable) fields on the model, usually because the struct has no exported fields.ErrSubQueryRequired
ErrSubQueryRequired
Message:
sub query requiredOccurs when: A clause expects a subquery value (e.g., using *DB as a condition argument) but none was provided.ErrInvalidData
ErrInvalidData
Message:
unsupported dataOccurs when: A value passed to a GORM method is of a type that cannot be handled (e.g., an unsupported type in Select args).ErrUnsupportedDriver
ErrUnsupportedDriver
Message:
unsupported driverOccurs when: db.SavePoint or db.RollbackTo is called and the dialector does not implement SavePointerDialectorInterface.ErrRegistered
ErrRegistered
Message:
registeredOccurs when: db.Use(plugin) is called with a plugin whose Name() is already registered.ErrInvalidField
ErrInvalidField
Message:
invalid fieldOccurs when: A referenced field name does not exist on the schema.ErrEmptySlice
ErrEmptySlice
Message:
empty slice foundOccurs when: An empty slice is passed to Create or a related operation that requires at least one element.ErrDryRunModeUnsupported
ErrDryRunModeUnsupported
Message:
dry run mode unsupportedOccurs when: Row() or Rows() is called while dry-run mode is active. Dry run can only generate SQL for operations that return a *DB; it cannot simulate raw row scanning.ErrInvalidDB
ErrInvalidDB
Message:
invalid dbOccurs when: db.DB() cannot resolve the underlying *sql.DB from the current connection pool — for example, when the pool is a custom type that does not implement GetDBConnector.ErrInvalidValue
ErrInvalidValue
Message:
invalid value, should be pointer to struct or sliceOccurs when: A non-pointer or otherwise unsuitable value is passed to a method that requires a pointer to a struct or slice (e.g., Association.Append with an un-addressable struct).ErrInvalidValueOfLength
ErrInvalidValueOfLength
Message:
invalid association values, length doesn't matchOccurs when: Association.Append or Association.Replace is called with a slice of values whose length does not match the number of parent records.ErrPreloadNotAllowed
ErrPreloadNotAllowed
Message:
preload is not allowed when count is usedOccurs when: Preload is combined with Count in the same query chain. Preloading makes no sense for a count query.ErrDuplicatedKey
ErrDuplicatedKey
Message:
duplicated key not allowedOccurs when: A Create or Save violates a unique key constraint. Only returned when Config.TranslateError is true and the dialector implements ErrorTranslator.ErrForeignKeyViolated
ErrForeignKeyViolated
Message:
violates foreign key constraintOccurs when: An insert or update violates a foreign key constraint. Only returned when Config.TranslateError is true and the dialector implements ErrorTranslator.ErrCheckConstraintViolated
ErrCheckConstraintViolated
Message:
violates check constraintOccurs when: An insert or update violates a CHECK constraint defined on the table. Only returned when Config.TranslateError is true and the dialector implements ErrorTranslator.