Skip to content

Cannot retrieve inserted row when using UUID as primary key #338

@korECM

Description

@korECM

Description

I'm encountering an issue when trying to create a record with a UUID primary key using Bob ORM. While the system works fine with auto-increment columns, it fails when using UUID as the primary key.

Environment

  • Bob ORM
  • MySQL database
  • Go

Database Schema

create table api_keys
(
    id          binary(16) default (uuid_to_bin(uuid(), 1)) not null
        primary key,
    `key`       varchar(64)                                 not null,
    secret      varchar(255)                                null,
    constraint api_keys_key_uniq
        unique (`key`)
)
    row_format = DYNAMIC;

Code Example

f := factory.New()
createdAPIKey, err := f.NewAPIKey().Create(ctx, writerBob)
require.NoError(t, err)

Error Message

cannot retrieve inserted row

Looking at the source code in dialect/mysql/table.go, it appears that the issue occurs because:

The system checks for autoIncrementColumn first, which is empty in this case
Then it falls back to uniqueSet, but fails to find it
This results in returning orm.ErrCannotRetrieveRow
Here's the relevant code from dialect/mysql/table.go:

if t.autoIncrementColumn != "" {
    lastID, err := results[i].LastInsertId()
    if err != nil {
        return nil, err
    }
    autoIncrArgs = append(autoIncrArgs, Arg(lastID))
} else {
    uIdx, uArgs := t.uniqueSet(w, val)
    if uIdx == -1 || len(uArgs) == 0 {
        return nil, orm.ErrCannotRetrieveRow
    }
    idArgs[uIdx] = append(idArgs[uIdx], ArgGroup(internal.ToAnySlice(uArgs)...))
}

Questions

  1. Is this a known limitation when using UUID as primary key?
  2. Is there a workaround available?
  3. If this is a bug, what would be the best approach to fix it?

Additional Context

The system works fine with auto-increment primary keys, but fails specifically with UUID primary keys. This seems to be related to how the ORM handles record retrieval after insertion when using non-auto-increment unique identifiers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions