我在MySQL 5.5中有一个数据库表,其定义如下:
CREATE TABLE IF NOT EXISTS `user_relationships` (
`_id` int(11) NOT NULL AUTO_INCREMENT,
`create_date` long NOT NULL,
`update_date` long NOT NULL,
`user_id` int(11) NOT NULL,
`related_user_id` int(11) NOT NULL,
`active` int(11) DEFAULT -1 NOT NULL ,
PRIMARY KEY (`_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
然而,当我对表运行查询时,我得到以下异常:
Exception: Save user_relationship record error on: insert into user_relationships(create_date, update_date, user_id, related_user_id) values(1421086506464, 1421086506464, 13, 13) - Adapter ExceptionMessage: SQLException: Field 'active' doesn't have a default value
create语句定义默认值-1:
`active` int(11) DEFAULT -1 NOT NULL ,
那么,为什么MySQL会抛出这个异常呢?如果我刮掉insert语句并从MySQL工作台运行它,则运行w / o异常。我还放弃了表并使用这个sql语句重建它,以确保使用了这个定义。
修改
来自MySQL Workbench的消息执行create table语句:
12:14:05 CREATE TABLE IF NOT EXISTS `user_relationships` ( `_id` int(11) NOT NULL AUTO_INCREMENT, `create_date` long NOT NULL, `update_date` long NOT NULL, `user_id` int(11) NOT NULL, `related_user_id` int(11) NOT NULL, `active` int(11) DEFAULT -1 NOT NULL , PRIMARY KEY (`_id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 0 row(s) affected 0.055 sec
12:14:05 CREATE INDEX ix_user_relationships__id ON user_relationships (_id) USING BTREE 0 row(s) affected Records: 0 Duplicates: 0 Warnings: 0 0.057 sec
12:14:05 CREATE INDEX ix_user_relationships_user_id ON user_relationships (user_id) USING BTREE 0 row(s) affected Records: 0 Duplicates: 0 Warnings: 0 0.059 sec
12:14:05 CREATE INDEX ix_user_relationships_related_user_id ON user_relationships (related_user_id) USING BTREE 0 row(s) affected Records: 0 Duplicates: 0 Warnings: 0 0.064 sec