Problem:
Your Go application utilizes two models, User and UserInfo, and you desire to establish a foreign key relationship between the UID field in UserInfo and the id field in User. Modifying the UserInfo model alone has proven ineffective.
Solution:
Instead of relying solely on the gorm annotations in the UserInfo model, you can define the foreign key constraint during database migration. Add the following line to your migration script:
db.Model(&models.UserInfo{}).AddForeignKey("u_id", "t_user(id)", "RESTRICT", "RESTRICT")
This line accomplishes the following:
The above is the detailed content of How to Create a Foreign Key Relationship with GORM in Go?. For more information, please follow other related articles on the PHP Chinese website!