Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for UUID as primary key #7

Open
llulioscesar opened this issue Jan 3, 2025 · 2 comments
Open

Add support for UUID as primary key #7

llulioscesar opened this issue Jan 3, 2025 · 2 comments

Comments

@llulioscesar
Copy link

Currently, there seems to be limited support for using UUID as a primary key in models. When trying to use UUID with the primary tag, the following error occurs:

ERRO [[email protected]/migrator.go:644] not handled migration String tag=primary time=02/01/2025 20:06
ERRO [[email protected]/migrator.go:723] migration String not handled for v=type tag=type:uuid f=user_id time=02/01/2025 20:06

Example code that produces the error:

type User struct {
    UserId    string    `korm:"primary;type:uuid;default:gen_random_uuid()"`
    FirstName string    `korm:"size:100;notnull"`
    Email     string    `korm:"size:150;unique"`
    CreatedAt time.Time `korm:"now"`
    UpdatedAt time.Time `korm:"now"`
}

Expected behavior:

  • The ability to use UUID as a primary key type
  • Support for PostgreSQL's UUID functions like gen_random_uuid()
  • Proper handling of the primary tag with string/UUID fields

Possible Solution:

Consider adding explicit support for UUID fields in the migrator, similar to how numeric primary keys are handled. This would be particularly useful for distributed systems where UUIDs are preferred over sequential IDs.

Additional context:

This feature would be beneficial for:

  • Distributed systems where sequential IDs are problematic
  • Applications requiring globally unique identifiers
  • Systems that need to generate IDs without central coordination

Environment:

korm version: v1.95.8
Go version: 1.22
Database: PostgreSQL

Would you be interested in contributions to implement this feature?

@kamalshkeir
Copy link
Owner

Hello @llulioscesar
It is intended to not handle UUIDs as primary keys, which is also a bad practice, can decrease database performance drastically, and we loose ordering.
Also primary doesn't exist as tag, it's pk or autoinc for primary key field, you can refer the documentation for supported tags.
So , you can create In addition to Id korm:"pk" (best practice) , a field named Uuid korm:"size:50;unique" so you can link other table to it using foreign keys.
Also if uuid detected in column name, the dashboard hanle auto generate like you see in the dashboard for User model, and from code you can use korm.GenerateUUID().
i also notice you are using UpdatedAt time.Time korm:"now" , the tag, should be "update" if you want auto updated time field
i may in the future add some triggers for uuid that support all dialects, but until, no special treatment for gen_random_uuid if other dialects doesn't support it. The goal is to be able to switch from sqlite to mysql, to postgres, without any problem.
I'm also working on a node manager, and load balancer, so you can synchronise and backup multiple databases whith different dialects.
Hope this help

@covrom
Copy link

covrom commented Jan 30, 2025

The uuid in pk is a best practice for large projects where your id can be generated not only in the database but also on the client side. It is not necessary to use id to keep order, it is not its responsibility. Also, easily predictable (ordered) id is an opportunity to hack your system very quickly and get access to protected data.

No need for gen_random_uuid(), id generation should be done explicitly in code on go or somewhere else. The type should not be string but https://github.com/google/uuid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants