2 回答
TA贡献1895条经验 获得超3个赞
你已经包含gorm.Model在你的结构中。这意味着您的模型迁移/数据库将给出错误:
Error 1075: Incorrect table definition; there can be only one auto column and it must be defined as a key
如果你gorm.Model从你的Topic结构中删除,你会很好。
package model
import (
`github.com/jinzhu/gorm`
)
type WithoutModel struct {
MyId int64 `gorm:"primary_key;auto_increment;not_null"`
Name string
}
func ModelSave(tx *gorm.DB) {
wo := WithoutModel{Name:"Without the model"}
tx.Save(&wo)
}
运行ModelSave几次后,我有:
MariaDB [gmodel]> select * from without_models;
+-------+-------------------+
| my_id | name |
+-------+-------------------+
| 1 | Without the model |
| 2 | Without the model |
+-------+-------------------+
2 rows in set (0.000 sec)
TA贡献1842条经验 获得超21个赞
gorm.Model
// gorm.Model 定义
type Model struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
- 2 回答
- 0 关注
- 410 浏览
添加回答
举报