dongshi2588 2019-08-26 21:48
浏览 339
已采纳

GORM数据库中的自动迁移将不必要的字段添加到SQL表

When I create my tables in the gorm database, it adds columns to the table that I don't want. I'm not sure how it's adding these extra fields. This causes me to run into an error that says, "pq: null value in column "user_id" violates not-null constraint". "user_id" is the unwanted column that gets added. I'm using gorm and postgreSQL.

I have a many to many relationship for my two tables. My first table is created properly and my second table, stores, is created with the provided fields plus two unwanted fields: "user_id" and "stores_id". I've tried removing the many to many relationship to see if that was the issue, I've tried dropping the tables and recreating them with different fields. Regardless, I have not been able to get rid of the two extra columns.

The first (working) table:

type User struct {
    gorm.Model
    ID int `json:"u_id"`
    Name string `json:"u_name"`
    Stores []Store `gorm:"many2many:stores;" json:"stores"`
}

When I execute '\d users', I get the following columns: id, created_at, updated_at, deleted_at, name.

The second (problematic) table:

type Stores struct {
    gorm.Model
    ID int `json:"s_id"`
    NUM int `gorm:"unique" json:"s_num"`
    Users []User `gorm:"many2many:user" json:"users"`
}

When I execute '\d' stores, I get the following columns: user_id, vehicle_id, id, created_at, updated_at, deleted_at, num.

I'm executing the creation of these tables through the following code:

db.AutoMigrate(&User{})
db.AutoMigrate(&Store{})

On another note, if I add gorm:"primary_key";auto_increment" to my ID values in my structs, I get the error "pq: column "user_id" appears twice in primary key constraint". I was able to get around this by removing the primary_key and auto_increment attributes, running AutoMigrate() and then adding it back in and running AutoMigrate() again - this was totally fine and working.

I've also tried manually inserting a user_id and store_id. This works fine, except that I'd have to generate new ones every time because they require uniqueness. I understand that the error "pq: null value in column "user_id" violates not-null constraint" is caused by the fact that I'm not providing a user_id or store_id when I'm creating my store. I'm simply confused why a user_id and store_id column is being generated at all, and I'm hoping I can fix that.

  • 写回答

2条回答 默认 最新

  • doujia1163 2019-08-27 16:26
    关注

    Fixed the duplicate ID errors by removing gorm.Model, as @(Akshaydeep Girl) pointed out what having gorm.Model entails. As for the random 'user_id' and 'store_id' that kept automatically being added, they were being added because of the many2many gorm relationship. I was able to remove those by switching the order of migration.

    func DBMigrate(db *gorm.DB) *gorm.DB {
        db.AutoMigrate(&Store{})
        db.AutoMigrate(&User{})
        return db
    }
    

    When I dropped both tables and re-compiled/ran my project with the new order of migration, the stores table was created without the random 'user_id' and 'store_id', and the users table didn't have those either.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建