kk1957 kk1957 - 13 days ago 5
MySQL Question

mysql error 1364 Field doesn't have a default values

My table looks like

create table try ( name varchar(8), CREATED_BY varchar(40) not null);


and then I have a trigger to auto populate the CREATED_BY field

create trigger autoPopulateAtInsert BEFORE INSERT on try for each row set new.CREATED_BY=user();


When I do an insert using

insert into try (name) values ('abc');


the entry is made in the table but I still get the error message

Field 'CREATED_BY' doesn't have a default value Error no 1364


Is there a way to suppress this error without making the field nullable AND without removing the triggfer? Otherwise my hibernate will see these exceptions ( even though the insertions have been made) and then application will crash.

Answer

set a default value for Created_By (empty varchar for example) and the trigger will update the value anyways.