Skip to content

Mismatch: WithResult() has no Error field, but docs show result.Error #7703

@JCast867

Description

@JCast867

Your Question

There appears to be a mismatch between the Generics API example in the Create docs and the implementation of WithResult() in v1.31.1.

Docs example (Generics API):

user := User{Name: "Jinzhu", Age: 18, Birthday: time.Now()}

// Create with result
result := gorm.WithResult()
err := gorm.G[User](db, result).Create(ctx, &user)
user.ID             // returns inserted data's primary key
result.Error        // returns error
result.RowsAffected // returns inserted records count

Code at v1.31.1:

// https://github.com/go-gorm/gorm/blob/v1.31.1/generics.go#L17
type result struct {
    Result       sql.Result
    RowsAffected int64
}

There is no Error field on that struct. Create(ctx, &user) returns error directly (as shown by the example's err variable).

Is the documentation intended to show result.Error for the generics WithResult usage, or should the docs be updated to reflect the actual API (i.e. check the returned err from Create, and read rows from result.RowsAffected and result.Result)?

The document you expected this should be explained

https://gorm.io/docs/create.html#Generics-API

(implementation reference: https://github.com/go-gorm/gorm/blob/v1.31.1/generics.go#L30)

Expected answer

Please clarify intended behavior and fix either:

  • Documentation: update the Generics-API Create example to remove result.Error and show checking the err returned by Create, and using result.RowsAffected / result.Result for driver info; or
  • Implementation: if the intended ergonomics are to read an error from the provided result object, add an Error field (and document its semantics) or change WithResult to accept/return a type that exposes Error.

I’m asking which is intended and request the docs (or code) be corrected accordingly.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions