【问题标题】:AutoMigrate does not generate self-referencing foreign keyAutoMigrate 不生成自引用外键
【发布时间】:2021-07-31 00:13:46
【问题描述】:

我正在尝试让自引用键在我的模型中工作。出于某种原因,只是没有创建外键关系。我尝试了一堆结构标签的变体,但都无济于事。

我有自己的 Mixin:

type Mixin struct {
    ID        uint      `json:"id"`
    CreatedAt time.Time `json:"created_at"`
    UpdatedAt time.Time `json:"updated_at"`
    Deleted   bool      `json:"deleted"`
}

问题出现在这个模型中,当我在 Postgres 上运行 AutoMigrate 时,没有创建 ParentTaskID 外键约束。

type Task struct {
    Mixin
    Content  string    `gorm:"not null; default:Empty Task" json:"content"`
    Deadline time.Time `json:"deadline,omitempty"`
    Priority uint      `json:"priority,omitempty"`
    Complete bool      `json:"complete,omitempty"`

    // Relations
    BucketID *uint   `gorm:"not null" json:"bucket_id"`
    Bucket   *Bucket `gorm:"foreignKey: BucketID" json:"-"`

    ParentTaskID *uint `gorm:"not null" json:"parent_task_id"`
    ParentTask   *Task `gorm:"foreignKey: ParentTaskID" json:"-"`
}

【问题讨论】:

    标签: postgresql go foreign-keys go-gorm self-reference


    【解决方案1】:

    我可以确认这似乎是 gorm 中的一个错误。我创建了an issue 和a test case 来重现该错误。

    您可以在 github 中订阅更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      相关资源
      最近更新 更多