Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用gormt再次生成数据表出错 #221

Open
solitudealma opened this issue Oct 5, 2022 · 6 comments
Open

使用gormt再次生成数据表出错 #221

solitudealma opened this issue Oct 5, 2022 · 6 comments

Comments

@solitudealma
Copy link

生成的struct无论是否使用gorm.Model,再次用gorm迁移生成的新数据表都不会在id这个字段设置自增,然后插入时报错,Error 1364: Field 'id' doesn't have a default value,就是说自增没有设置到

以下是我的数据表结构和生成的结构体

CREATE TABLE IF NOT EXISTS `users` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` TIMESTAMP NOT NULL DEFAULT 0,
  `username` VARCHAR(64) NOT NULL DEFAULT '',
  `nickname` VARCHAR(64) NOT NULL DEFAULT '',
  `password` VARCHAR(255) NOT NULL DEFAULT '',
  `school` VARCHAR(32) NOT NULL DEFAULT '',
  `email` VARCHAR(255) NOT NULL DEFAULT '',
  `role` VARCHAR(64) NOT NULL DEFAULT 'student' COMMENT '\'student\',\'teacher\',\'admin\'',
  `email_verified_at` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
  `status` TINYINT NOT NULL DEFAULT 0 COMMENT '-1: lock\n0: normal\n1: need verify by admin',
  `remember_token` VARCHAR(100) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE INDEX `uq_email` (`deleted_at` ASC, `email` ASC),
  UNIQUE INDEX `uq_username` (`deleted_at` ASC, `username` ASC))
ENGINE = InnoDB;
gorm.Model
Username        string    `gorm:"uniqueIndex:uq_username;column:username;type:varchar(64);not null;default:''" json:"username"`
Nickname        string    `gorm:"column:nickname;type:varchar(64);not null;default:''" json:"nickname"`
Password        string    `gorm:"column:password;type:varchar(255);not null;default:''" json:"-"`
School          string    `gorm:"column:school;type:varchar(32);not null;default:''" json:"school"`
Email           string    `gorm:"uniqueIndex:uq_email;column:email;type:varchar(255);not null;default:''" json:"email"`
Role            string    `gorm:"column:role;type:varchar(64);not null;default:student;comment:''student','teacher','admin''" json:"role"`
EmailVerifiedAt time.Time `gorm:"column:email_verified_at;type:timestamp;not null;default:'0000-00-00 00:00:00'" json:"emailVerifiedAt"`
Status          int8      `gorm:"column:status;type:tinyint;not null;default:0;comment:'-1: lock0: normal1: need verify by admin'" json:"status"`
RememberToken   string    `gorm:"column:remember_token;type:varchar(100);not null;default:''" json:"rememberToken"`

希望您有空的时候可以看看这个问题

@solitudealma
Copy link
Author

好像发现了问题,当不需要gorm.Model的部分字段时,ID的类型是uint64,虽然说这个类型在64位系统上与uint并没有是等价的,但应该还是有区别的。作者可以试试把主键设置为uint,跟随gorm 。还有就是看到别人说auto_increment是type下的一个参数,应该放在type里面,试了一下发现都可以,后续应该也没啥问题

@solitudealma
Copy link
Author

好像数据表结构是int的时候没有问题, 加上unsigned之后就会变成uint64。ID类型是int和uint都可以设置成功

@xxjwxc
Copy link
Owner

xxjwxc commented Oct 8, 2022

这个问题是不是gorm本身的问题?

@solitudealma
Copy link
Author

我也不太清楚底层的 我试了一下换成unit就可以在可视化工具看到设置成功了自增 然后插入数据也不会报没有设置默认值的错误

@xxjwxc
Copy link
Owner

xxjwxc commented Oct 8, 2022

sql语句有没有?我试下

@solitudealma
Copy link
Author

sql语句有没有?我试下

现在没有电脑 我是直接用的这个sql 也是一样的 没有改动过

https://github.com/SDIBTACM/Miscellaneous/blob/master/Database/create-sql.sql

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants