go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.87k stars 3.93k forks source link

sqlite: primary key contstraint gets lost when embedded struct also contains primary key #6945

Open phil9909 opened 6 months ago

phil9909 commented 6 months ago

GORM Playground Link

https://github.com/go-gorm/playground/pull/712

Description

For the following model

type Common struct {
    TenantID uint `gorm:"primarykey"`
}

type User struct {
    Common
    UserID string `gorm:"primarykey"`
    // ...
}

this SQL DDL is generated

CREATE TABLE `users` (`tenant_id` integer PRIMARY KEY AUTOINCREMENT,`user_id` text, [...])

Notice: The user_id is missing the PRIMARY KEY constraint

It worked fine with gorm.io/driver/sqlite v1.5.3 but is broken in gorm.io/driver/sqlite v1.5.4

gg1229505432 commented 6 months ago

Generally, An SQL statement can have only one primary key

phil9909 commented 6 months ago

Hi @gg1229505432, thank you for your replay.

Generally, An SQL statement can have only one primary key

Yes, a SQL relation (I guess this is what you meant with statement, because a statement does not have a primary key, it might specify one, if it's a DDL statement) can only have one primary key, hence the name primary key.

But that is completely unrelated to this issue. I don't want multiple primary keys, that would be insane :sweat_smile:

What I want is a composite primary key, which as per documentation gorm supports: https://gorm.io/docs/composite_primary_key.html and as I said already: It was even working in older versions

DPrince87 commented 3 months ago

I have the same problem with 1.5.6. Downgrading to 1.5.3 solves this.