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

HasMany: ForeignKey as String #5924

Closed
maltegrosse opened this issue Dec 16, 2022 · 1 comment
Closed

HasMany: ForeignKey as String #5924

maltegrosse opened this issue Dec 16, 2022 · 1 comment
Assignees
Labels
type:question general questions

Comments

@maltegrosse
Copy link

maltegrosse commented Dec 16, 2022

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 go-gorm/postgres#111

@maltegrosse maltegrosse added the type:question general questions label Dec 16, 2022
@maltegrosse
Copy link
Author

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

	
}

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

No branches or pull requests

2 participants