外键命名过于严格问题,很多直觉能够成功的情况,皆迁移失败

https://gorm.io/zh_CN/docs/conventions.html

只有这一种命名方式,能够成功

1成功

// 用户表
type ManUser struct {
    ManUserId string `gorm:"primaryKey" `
    RealName  string      
    beans.BaseModel
}

// 用户操作记录表
type ManLog struct {
    LogId     string   `gorm:"primaryKey" `
    ManUserId string  
    ManUser   *ManUser  
    beans.BaseModel
}

2失败

补充了foreignkey、references,创建失败,外键约束创建错误

// 用户表
type ManUser struct {
    ManUserId string `gorm:"primaryKey"  `
    RealName  string  
    beans.BaseModel
}

// 用户操作记录表
type ManLog struct {
    LogId     string   `gorm:"primaryKey" `                        
    ManUserId string                                 
    ManUser   *ManUser `gorm:"foreignkey:ManUserId;references:ManUserId" `  // 补充foreignkey、references,失败
    beans.BaseModel
}

3失败

重命名ManUserId=》UserId,清空foreignkey、references,创建失败,外键约束创建错误

// 用户表
type ManUser struct {
    UserId   string `gorm:"primaryKey" ` 
    RealName string `json:"realName"` 
    beans.BaseModel
}

// 用户操作记录表
type ManLog struct {
    LogId   string   `gorm:"primaryKey" `               
    UserId  string   `gorm:"index" `     // 重命名ManUserId=》UserId              
    ManUser *ManUser   // 清空foreignkey、references
    beans.BaseModel
}

4失败

重命名ManUserId=》UserId,补充foreignkey、references,创建失败,外键约束创建错误

// 用户表
type ManUser struct {
    UserId   string `gorm:"primaryKey" ` 
    RealName string `json:"realName"` 
    beans.BaseModel
}

// 用户操作记录表
type ManLog struct {
    LogId   string   `gorm:"primaryKey" `               
    UserId  string   `gorm:"index" `                    
    ManUser *ManUser `gorm:"foreignkey:UserId;references:UserId"`//补充foreignkey、references,失败
    beans.BaseModel
}

5失败

重命名ManUser=》ManUserInfo,,补充foreignkey、references,创建失败,外键约束创建错误

// 用户表
type ManUser struct {
    UserId   string `gorm:"primaryKey" ` 
    RealName string `json:"realName"` 
    beans.BaseModel
}

// 用户操作记录表
type ManLog struct {
    LogId   string   `gorm:"primaryKey" `               
    UserId  string   `gorm:"index" `                    
    ManUserInfo *ManUser `gorm:"foreignkey:UserId;references:UserId" `// 重命名ManUserInfo,补充foreignkey、references,失败
    beans.BaseModel
}

错误记录:

there is no unique constraint matching given keys for referenced table "tb_man_logs"

外键修改到错误的表上:

ALTER TABLE "tb_man_users" ADD CONSTRAINT "fk_tb_man_logs_man_user_info" FOREIGN KEY ("man_user_id") REFERENCES "tb_man_logs"("man_user_id")

问题: 1、是否无法重命名主键,规则只有两种格式,一种是ID,另外一种是XId? 2、关联表,是否无法重命名,只能 ManUser ManUser ,不能 ManUserInfo ManUser?

https://gorm.io/zh_CN/docs/conventions.html

我是否漏看了某些文档命名约定?

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking