0

I'm trying to run this delete script:

DELETE auth, cust
FROM customer cust 
JOIN authentication_token auth ON auth.customer_id = cust.id
WHERE cust.email = '[email protected]';

I'm getting the error:

Cannot delete or update a parent row: a foreign key constraint fails (`my_table`.`authentication_token`, CONSTRAINT `authentication_token_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`))

I've tried to change the order of the DELETE statement (cust, auth), and I've tried to change the order of the join, but still the same error.

Is this possible, or will I have to temporary disable constraints?

Table structure:

CREATE TABLE `customer` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `email` varchar(255) DEFAULT NULL,
  `firstName` varchar(255) DEFAULT NULL,
  `lastName` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `passwordSalt` varchar(255) DEFAULT NULL,
  `phoneNumber` varchar(255) DEFAULT NULL,
  `addressLine1` varchar(255) DEFAULT NULL,
  `addressLine2` varchar(255) DEFAULT NULL,
  `city` varchar(255) DEFAULT NULL,
  `country` varchar(255) DEFAULT NULL,
  `county` varchar(255) DEFAULT NULL,
  `postCode` varchar(255) DEFAULT NULL,
  `registeredOn` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE `authentication_token` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `value` varchar(255) DEFAULT NULL,
  `createdOn` datetime DEFAULT NULL,
  `customer_id` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `customer_id` (`customer_id`),
  CONSTRAINT `authentication_token_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`)
)
2
  • can you share parent and child table structure ? Commented Jan 22, 2018 at 14:23
  • @Ravi updated question. Commented Jan 22, 2018 at 14:29

2 Answers 2

1

Alter your table definition:

CREATE TABLE `authentication_token` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `value` varchar(255) DEFAULT NULL,
  `createdOn` datetime DEFAULT NULL,
  `customer_id` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `customer_id` (`customer_id`),
  CONSTRAINT `authentication_token_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`) ON DELETE CASCADE
)

And then just run this SQL:

DELETE FROM cust WHERE cust.email = '[email protected]';

When you delete the records from cust, the relevant records in authentication_token will be deleted automatically.

Sign up to request clarification or add additional context in comments.

1 Comment

That's great! I didn't know about automatic cascaded deletes. That's a great solution, much better than what I was trying to do. Thank you!
0

Cannot delete or update a parent row

It means, you can't update/delete parent table, since, reference still exist in some other child table(s).

If you wanted to delete from both table, then best solution would be altering your table to add

ON CASCADE DELETE

It will ensure that all the references rows gets deleted.

CONSTRAINT `authentication_token_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`) ON CASCADE DELETE

3 Comments

I'm not specifying columns, I'm specifying tables.
@user7862512 DELETE auth, cust are they table ?
yep. They're tables which I've aliased in the from and join statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.