Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3953446
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:58:33+00:00 2026-05-20T01:58:33+00:00

I have a HABTM association between user and role. User can be an admin

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:58:34+00:00Added an answer on May 20, 2026 at 1:58 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working HABTM association between the models Posts and Users... the posts_users
I have a User and Company model. There are three possible roles that a
I have a HABTM relationship between Videos and Campaigns in Rails which means the
I have a script that appends some rows to a table. One of the
I must be missing something, when trying to define a HABTM association with 2
I have two models, users and themes, that I'm currently joining in a HABTM
In an ActiveRecord (CakePHP flavored) setup I have a HasAndBelongsToMany association setup on Videos
i have a User with attributes User.id i have an object with Object.id, Object.name
I have a weird situation involving the need of a double inner join. I
i have a input tag which is non editable, but some times i need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.