I have an INSERT
statement that looks like this:
INSERT INTO officer (officer_number,
name,
bank_id)
VALUES ('',
'',
8)
ON DUPLICATE KEY UPDATE officer_number = '',
name = '',
bank_id = 8,
id = LAST_INSERT_ID(id)
This way of doing it has been working just fine. It stopped working when I added the following trigger:
CREATE TRIGGER officer_update BEFORE UPDATE ON `officer`
FOR EACH ROW SET NEW.updated_at = NOW(), NEW.created_at = OLD.created_at
It's not that the officer
record isn't getting inserted. It just seems that the trigger is hijacking LAST_INSERT_ID()
or something. I say this because the next query that's executed is this:
INSERT INTO account (import_id,
branch_id,
account_number,
officer_id,
customer_id,
open_date,
maturity_date,
interest_rate,
balance,
opening_balance)
VALUES ('123',
'4567',
'789',
'0', # This is the officer id which is of course invalid
'321',
'1992-04-22',
'2012-05-22',
'0.0123',
'0',
'10000')
Since I've run dozens of successful imports with the same exact file, I haven't changed my code, and now my imports aren't working after I added this trigger, I must deduce that the trigger is the culprit. I had a similar situation with another table and removing the trigger fix the problem.
So my questions are:
- Can someone explain what, specifically, is causing my officer id to get set to 0?
- What's a good solution to this problem?
I have another trigger on officer.created_at
(and a lot of other tables' created_at
s) and I would prefer to avoid some sort of awkward solution where I have a trigger on created_at
but a DEFAULT CURRENT_TIMESTAMP
on updated_at
. For some reason, MySQL only allows one auto-timestamp per table, so I can't do CURRENT_TIMESTAMP
for both created_at
and updated_at
.
Here is the SHOW CREATE TABLE
for officer
:
CREATE TABLE `officer` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`officer_number` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`bank_id` bigint(20) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `officer_number` (`officer_number`,`name`),
UNIQUE KEY `officer_number_2` (`officer_number`,`bank_id`),
KEY `bank_id` (`bank_id`),
CONSTRAINT `officer_ibfk_1` FOREIGN KEY (`bank_id`) REFERENCES `bank` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=102735 DEFAULT CHARSET=latin1
SHOW CREATE TABLE officer
and the query which works incorrectly? Your title saysON DUPLICATE KEY UPDATE
but there is no such query in the post. – JuleeLAST_INSERT_ID()
? seems to be working fine on my machine MySql 5.1.53 – Noninterference