原因
mysql新建表时,设置自增长的字段没有设置成主键
通常我们这么做(我是用powerdesigner设计后导出)
create table tb_department
(
Id bigint not null auto_increment comment 'id',
Name varchar(60) not null default '' comment '名称',
Short_Name varchar(40) not null default '' comment '简称'
);
alter table tb_department comment '主体表-部门';
alter table tb_department
add primary key (Id);
设置外键放在最后,这样执行是不行的,在生成自增长字段是没有同时设置主键,所以报错
解决
把primary key (Id)提到create table里面
bigint not null auto_increment comment 'id', primary key (Id)
即以上改成
/*==============================================================*/
/* Table: tb_department */
/*==============================================================*/
create table tb_department
(
Id bigint not null auto_increment comment 'id', primary key (Id),
Name varchar(60) not null default '' comment '名称',
Short_Name varchar(40) not null default '' comment '简称'
);
alter table tb_department comment '主体表-部门';