字段默认值不生效

来源:6-9 雪花算法与新增功能

華晧

2021-04-11

新增电子书报错Cause: java.sql.SQLIntegrityConstraintViolationException: Column ‘doc_count’ cannot be null
; Column ‘doc_count’ cannot be null; nested exception is java.sql.SQLIntegrityConstraintViolationException: Column ‘doc_count’ cannot be null] with root cause

doc-count,view_count,vote_count的默认值都不生效
创建数据表sql

create table `ebook` (
  `id` bigint not null comment 'id',
  `name` varchar(50) comment '名称',
  `category1_id` bigint comment '分类1',
  `category2_id` bigint comment '分类2',
  `description` varchar(200) comment '描述',
  `cover` varchar(200) comment '封面',
  `doc_count` int not null default 0 comment '文档数',
  `view_count` int not null default 0 comment '阅读数',
  `vote_count` int not null default 0 comment '点赞数',
  primary key (`id`)
) engine=innodb default charset=utf8mb4 comment='电子书';
写回答

3回答

甲蛙

2021-04-12

课程后面我们讲到这个问题

因为mbyatis生成的代码是全字段的,所以insert 或 update都是带全字段的

如果insert SQL里写了字段,且给的值是null,这种情况就会存null,default 就失效了

解决方法就是在java代码里设置下值 0,而不用default 0

1
1
華晧
好的,谢谢老师
2021-04-12
共1条回复

劝退专业户

2021-04-21

好家伙我直接把在数据库"不是null"给取消了XDhhhh

0
0

華晧

提问者

2021-04-11

在MySQL里默认值可以生效,在项目里就不生效

0
0

Spring Boot+Vue3前后端分离,实战wiki知识库系统

一课掌握前后端最火框架,更有职场竞争力

2524 学习 · 1671 问题

查看课程