Trigger not firing if insert is inside a transaction
I wrote a trigger to automatically update a date on donator table row after inserting a new row on my donation table. The problem is, it is not updating the date in donator as I expected
The trigger seems to work if I do an insert outside a transaction, though.
Here's the trigger:
delimiter $$
CREATE TRIGGER updateDate AFTER INSERT ON donation
FOR EACH ROW
UPDATE donator
SET Next_Date = DATE_ADD(NEW.Donation_Date, INTERVAL 3 MONTH)
WHERE id_Donator = NEW.Donator_id_Donator
$$
And here's the transaction:
DELIMITER $$
CREATE PROCEDURE RegisterDonation (in QuantityArg INT, in Donation_DateArg DATE, in Donator_idArg INT)
BEGIN
DECLARE exit handler for SQLEXCEPTION
BEGIN
SHOW ERRORS LIMIT 1;
SHOW WARNINGS;
ROLLBACK;
END;
DECLARE exit handler for SQLWARNING
BEGIN
SHOW ERRORS LIMIT 1;
SHOW WARNINGS;
ROLLBACK;
END;
START TRANSACTION;
set @next_id := ifnull((select max(Donation_id) from donation) + 1, 1);
INSERT INTO donation(Donation_id, Quantity, Donation_Date, Donator_id_Donator) VALUES
(@next_id, QuantityArg, Donation_DateArg, Donator_idArg);
COMMIT;
END
$$
I know I could just update the date inside the transaction but I have other triggers and it doesn't sound like a good solution to just copy paste the code into every single transaction. Is there a better way to do this?
mysql triggers transactions mysql-workbench
add a comment |
I wrote a trigger to automatically update a date on donator table row after inserting a new row on my donation table. The problem is, it is not updating the date in donator as I expected
The trigger seems to work if I do an insert outside a transaction, though.
Here's the trigger:
delimiter $$
CREATE TRIGGER updateDate AFTER INSERT ON donation
FOR EACH ROW
UPDATE donator
SET Next_Date = DATE_ADD(NEW.Donation_Date, INTERVAL 3 MONTH)
WHERE id_Donator = NEW.Donator_id_Donator
$$
And here's the transaction:
DELIMITER $$
CREATE PROCEDURE RegisterDonation (in QuantityArg INT, in Donation_DateArg DATE, in Donator_idArg INT)
BEGIN
DECLARE exit handler for SQLEXCEPTION
BEGIN
SHOW ERRORS LIMIT 1;
SHOW WARNINGS;
ROLLBACK;
END;
DECLARE exit handler for SQLWARNING
BEGIN
SHOW ERRORS LIMIT 1;
SHOW WARNINGS;
ROLLBACK;
END;
START TRANSACTION;
set @next_id := ifnull((select max(Donation_id) from donation) + 1, 1);
INSERT INTO donation(Donation_id, Quantity, Donation_Date, Donator_id_Donator) VALUES
(@next_id, QuantityArg, Donation_DateArg, Donator_idArg);
COMMIT;
END
$$
I know I could just update the date inside the transaction but I have other triggers and it doesn't sound like a good solution to just copy paste the code into every single transaction. Is there a better way to do this?
mysql triggers transactions mysql-workbench
@krishKM there is a well knownifnull()
function: dev.mysql.com/doc/refman/8.0/en/…
– Madhur Bhaiya
Nov 25 '18 at 12:17
@MadhurBhaiya thx.. added to my list :)
– krish KM
Nov 25 '18 at 12:20
1
I cannot reproduce your problem and I would expect a trigger to fire within a transaction. Can you add the initial set up for donators and some call statements to back up your claim.BTW if you are expecting the trigger to create a donator record then an update statement won't do that, your logic assumes donator records exist before donations are accepted.
– P.Salmon
Nov 25 '18 at 12:37
Welp, I fixed it. Closed workbench, restarted computer, opened workbench, tried it and suddenly the trigger is working. Why, I don't know, but I lost 2 hours debugging this.
– C. Rib
Nov 25 '18 at 16:34
add a comment |
I wrote a trigger to automatically update a date on donator table row after inserting a new row on my donation table. The problem is, it is not updating the date in donator as I expected
The trigger seems to work if I do an insert outside a transaction, though.
Here's the trigger:
delimiter $$
CREATE TRIGGER updateDate AFTER INSERT ON donation
FOR EACH ROW
UPDATE donator
SET Next_Date = DATE_ADD(NEW.Donation_Date, INTERVAL 3 MONTH)
WHERE id_Donator = NEW.Donator_id_Donator
$$
And here's the transaction:
DELIMITER $$
CREATE PROCEDURE RegisterDonation (in QuantityArg INT, in Donation_DateArg DATE, in Donator_idArg INT)
BEGIN
DECLARE exit handler for SQLEXCEPTION
BEGIN
SHOW ERRORS LIMIT 1;
SHOW WARNINGS;
ROLLBACK;
END;
DECLARE exit handler for SQLWARNING
BEGIN
SHOW ERRORS LIMIT 1;
SHOW WARNINGS;
ROLLBACK;
END;
START TRANSACTION;
set @next_id := ifnull((select max(Donation_id) from donation) + 1, 1);
INSERT INTO donation(Donation_id, Quantity, Donation_Date, Donator_id_Donator) VALUES
(@next_id, QuantityArg, Donation_DateArg, Donator_idArg);
COMMIT;
END
$$
I know I could just update the date inside the transaction but I have other triggers and it doesn't sound like a good solution to just copy paste the code into every single transaction. Is there a better way to do this?
mysql triggers transactions mysql-workbench
I wrote a trigger to automatically update a date on donator table row after inserting a new row on my donation table. The problem is, it is not updating the date in donator as I expected
The trigger seems to work if I do an insert outside a transaction, though.
Here's the trigger:
delimiter $$
CREATE TRIGGER updateDate AFTER INSERT ON donation
FOR EACH ROW
UPDATE donator
SET Next_Date = DATE_ADD(NEW.Donation_Date, INTERVAL 3 MONTH)
WHERE id_Donator = NEW.Donator_id_Donator
$$
And here's the transaction:
DELIMITER $$
CREATE PROCEDURE RegisterDonation (in QuantityArg INT, in Donation_DateArg DATE, in Donator_idArg INT)
BEGIN
DECLARE exit handler for SQLEXCEPTION
BEGIN
SHOW ERRORS LIMIT 1;
SHOW WARNINGS;
ROLLBACK;
END;
DECLARE exit handler for SQLWARNING
BEGIN
SHOW ERRORS LIMIT 1;
SHOW WARNINGS;
ROLLBACK;
END;
START TRANSACTION;
set @next_id := ifnull((select max(Donation_id) from donation) + 1, 1);
INSERT INTO donation(Donation_id, Quantity, Donation_Date, Donator_id_Donator) VALUES
(@next_id, QuantityArg, Donation_DateArg, Donator_idArg);
COMMIT;
END
$$
I know I could just update the date inside the transaction but I have other triggers and it doesn't sound like a good solution to just copy paste the code into every single transaction. Is there a better way to do this?
mysql triggers transactions mysql-workbench
mysql triggers transactions mysql-workbench
asked Nov 25 '18 at 12:05
C. RibC. Rib
667
667
@krishKM there is a well knownifnull()
function: dev.mysql.com/doc/refman/8.0/en/…
– Madhur Bhaiya
Nov 25 '18 at 12:17
@MadhurBhaiya thx.. added to my list :)
– krish KM
Nov 25 '18 at 12:20
1
I cannot reproduce your problem and I would expect a trigger to fire within a transaction. Can you add the initial set up for donators and some call statements to back up your claim.BTW if you are expecting the trigger to create a donator record then an update statement won't do that, your logic assumes donator records exist before donations are accepted.
– P.Salmon
Nov 25 '18 at 12:37
Welp, I fixed it. Closed workbench, restarted computer, opened workbench, tried it and suddenly the trigger is working. Why, I don't know, but I lost 2 hours debugging this.
– C. Rib
Nov 25 '18 at 16:34
add a comment |
@krishKM there is a well knownifnull()
function: dev.mysql.com/doc/refman/8.0/en/…
– Madhur Bhaiya
Nov 25 '18 at 12:17
@MadhurBhaiya thx.. added to my list :)
– krish KM
Nov 25 '18 at 12:20
1
I cannot reproduce your problem and I would expect a trigger to fire within a transaction. Can you add the initial set up for donators and some call statements to back up your claim.BTW if you are expecting the trigger to create a donator record then an update statement won't do that, your logic assumes donator records exist before donations are accepted.
– P.Salmon
Nov 25 '18 at 12:37
Welp, I fixed it. Closed workbench, restarted computer, opened workbench, tried it and suddenly the trigger is working. Why, I don't know, but I lost 2 hours debugging this.
– C. Rib
Nov 25 '18 at 16:34
@krishKM there is a well known
ifnull()
function: dev.mysql.com/doc/refman/8.0/en/…– Madhur Bhaiya
Nov 25 '18 at 12:17
@krishKM there is a well known
ifnull()
function: dev.mysql.com/doc/refman/8.0/en/…– Madhur Bhaiya
Nov 25 '18 at 12:17
@MadhurBhaiya thx.. added to my list :)
– krish KM
Nov 25 '18 at 12:20
@MadhurBhaiya thx.. added to my list :)
– krish KM
Nov 25 '18 at 12:20
1
1
I cannot reproduce your problem and I would expect a trigger to fire within a transaction. Can you add the initial set up for donators and some call statements to back up your claim.BTW if you are expecting the trigger to create a donator record then an update statement won't do that, your logic assumes donator records exist before donations are accepted.
– P.Salmon
Nov 25 '18 at 12:37
I cannot reproduce your problem and I would expect a trigger to fire within a transaction. Can you add the initial set up for donators and some call statements to back up your claim.BTW if you are expecting the trigger to create a donator record then an update statement won't do that, your logic assumes donator records exist before donations are accepted.
– P.Salmon
Nov 25 '18 at 12:37
Welp, I fixed it. Closed workbench, restarted computer, opened workbench, tried it and suddenly the trigger is working. Why, I don't know, but I lost 2 hours debugging this.
– C. Rib
Nov 25 '18 at 16:34
Welp, I fixed it. Closed workbench, restarted computer, opened workbench, tried it and suddenly the trigger is working. Why, I don't know, but I lost 2 hours debugging this.
– C. Rib
Nov 25 '18 at 16:34
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467256%2ftrigger-not-firing-if-insert-is-inside-a-transaction%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467256%2ftrigger-not-firing-if-insert-is-inside-a-transaction%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@krishKM there is a well known
ifnull()
function: dev.mysql.com/doc/refman/8.0/en/…– Madhur Bhaiya
Nov 25 '18 at 12:17
@MadhurBhaiya thx.. added to my list :)
– krish KM
Nov 25 '18 at 12:20
1
I cannot reproduce your problem and I would expect a trigger to fire within a transaction. Can you add the initial set up for donators and some call statements to back up your claim.BTW if you are expecting the trigger to create a donator record then an update statement won't do that, your logic assumes donator records exist before donations are accepted.
– P.Salmon
Nov 25 '18 at 12:37
Welp, I fixed it. Closed workbench, restarted computer, opened workbench, tried it and suddenly the trigger is working. Why, I don't know, but I lost 2 hours debugging this.
– C. Rib
Nov 25 '18 at 16:34