Skip to content

FindOrCreate Behavior With Double Pointer #4159

Closed
@cameron-dunn-silo

Description

GORM Playground Link

go-gorm/playground#296

Description

If FindOrCreate is given a double pointer (single value) for the second argument, conds ...interface{}, the behavior is unexpected and potentially confusing. When a double pointer is given, the behavior is:

  • No error is returned
  • A record is inserted in the DB which does not include fields from underlying value of the conds parameter. The inserted record seems to be blank except for fields which GORM itself handles (e.g. created_at).
    • These records are included if a single pointer is given.
    • If the inserted record violates a constraint it is of course not inserted, but still no error is returned. This differs from the behavior of Create, and I think it also differs from the behavior of FindOrCreate when using a single pointer.
      • This behavior is demonstrated in the second commit of the playground, although it fails just because sqlite doesn't support constraints. Locally it does fail for postgres.

I do not expect this behavior because:

  • Create successfully creates a record with the underlying values when given a double pointer.
  • GORM V1 FindOrCreate creates a record with the underlying values when given a double pointer.

I think better behavior would be any of (in order):

  • Create the record using the underlying values & return any errors from the DB.
  • Do not insert a record at all and return an error
  • Do not insert a record and panic

I can work around this easily enough, but this may be the source of hidden bugs for anyone migrating from GORM V1 or not testing their code completely.

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions