Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting duplicate records in join table

I have a HABTM association between user and role.

User can be an admin (role_id = 1) or a user (role_id = 2) for roles.

In the join table, roles_users, I have some redundant records. For ex:

enter image description here

I want to remove the duplicate records such as 1:1, 2:4.

Two questions:

  1. Where's the best place to execute the sql script that removes the dups -- migration? script?

  2. What is the sql query to remove the dups?

like image 823
keruilin Avatar asked Dec 07 '22 23:12

keruilin


1 Answers

CREATE TABLE roles_users2 LIKE roles_users; -- this ensures indexes are preserved
INSERT INTO roles_users2 SELECT DISTINCT * FROM roles_users; 
DROP TABLE roles_users;
RENAME TABLE roles_users2 TO roles_users;

and for the future, to prevent duplicate rows

ALTER TABLE roles_users ADD UNIQUE INDEX (role_id, user_id);

Or, you can do all of it in one step with ALTER TABLE IGNORE:

ALTER IGNORE TABLE roles_users ADD UNIQUE INDEX (role_id, user_id);

IGNORE is a MySQL extension to standard SQL. It controls how ALTER TABLE works if there are duplicates on unique keys in the new table or if warnings occur when strict mode is enabled. If IGNORE is not specified, the copy is aborted and rolled back if duplicate-key errors occur. If IGNORE is specified, only the first row is used of rows with duplicates on a unique key. The other conflicting rows are deleted. Incorrect values are truncated to the closest matching acceptable value.

like image 156
John Douthat Avatar answered Dec 10 '22 12:12

John Douthat