Your Question
Is it possible to use a String as a specific ForeignKey ? Using PostgreSQL Adapter
- Golang 1.19.1 linux
- gorm.io/driver/postgres v1.4.5
- gorm.io/gorm v1.24.2
- postgres:14.1
The document you expected this should be explained
https://gorm.io/docs/has_many.html offers the possibility to change the default foreignKey.
Example:
type A struct{
Id int64 `gorm:"primaryKey"`
Bs []B `gorm:"foreignKey:MyID"`
}
type B struct{
MyID string `gorm:"primaryKey"`
}
...
var models = []interface{}{A{},B{}}
db.AutoMigrate(models...)
Create on postgres db level:
- A: id:bigint
- B: my_id: bigint (!)
Expected answer
The primary key of B should be a String as defined in the gorm syntax.... Or am I doing something wrong?
P.S. Is this related to https://github.com/go-gorm/postgres/pull/111
Comment From: maltegrosse
solved it: B needs a int field for reference....
type A struct{
Id int64 `gorm:"primaryKey"`
Bs []B `gorm:"foreignKey:RefID"`
}
type B struct{
MyID string `gorm:"primaryKey"`
RefID int64
}