dongpao9437 2019-09-13 19:30
浏览 396

gorm中的外键和关联未正确创建

I tried to make models Association ForeignKey on PostgreSQL for the Person it belongs to Data and Data belongs to Person this is my struct

type (
    Data struct {
        ID          uint `gorm:"auto_increment"`
        PersonID    uint
        Person      *Person `gorm:"foreignkey:id;association_foreignkey:PersonID"`
        Birthday    *time.Time
        Salary      float64 `gorm:"type:money"`
    }

    Person struct {
        gorm.Model
        Email    string `gorm:"type:varchar(100);unique_index;not null"`
        Password string `gorm:"type:varchar(100);not null"`
        Role     string `gorm:"type:varchar(30);not null"`
        DataID   uint
        Data     *Data `gorm:"foreignkey:id;association_foreignkey:DataID"`
    }
)

I want to on Data table has Person_id is to be foreign key from Person table, and as well the Person table has Data_id to be foreign key from the data table after I see on ER Diagram on my DBeaver, those are not created as I wish :( it is not any relationship on ER Diagram, I am sure my Association on above wrong, can anyone give me some info to fix that?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 python代码,帮调试
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条