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

  • SEARCH
  • Home
  • 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 6121105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:44:45+00:00 2026-05-23T15:44:45+00:00

I’m just starting to learn Rails migrations. I have a table called tags with

  • 0

I’m just starting to learn Rails migrations. I have a table called tags with a column name [string] (and with the auto-generated id column). I want to migrate it so that the table uses the name column as the id and primary key. How do I do this making sure all existing records work with the new schema?

  • 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-23T15:44:46+00:00Added an answer on May 23, 2026 at 3:44 pm

    The approach is straightforward, but the execution seems risky. getting the migration up working is fairly straight forward, but the down? not sure.

    1. Make sure that your name column is unique across all records
    2. Find all other objects that store a reference to your object and change those from id to name.

    If you find that the column is not unique you’ll have to come up with some kind of plan to change that…like name+id in the name field for duplicates or something similar. All of this can be done from a migration, of course.

    I suggest leaving the id column there but only use it to resolve issues you find along the way

    By way of example, assume this is your model around the tags table:

    class Tag < ActiveRecord::Base
    end
    

    Assume you have at least this other model, Thing, that has a simple association with the Tag model:

    class Thing < ActiveRecord::Base
       has_one :tag
    end
    

    There is a tag_id column on the things table. I’d add a column called tag_name, change the association to have a foreign key pointing to this new column…

    class Thing < ActiveRecord::Base
      has_one :tag, :foreign_key => "tag_name"
    end
    

    This tells Rails that the association between these things is through the new column. By convention it would look for “tag_id”.

    Given this change you can go into the migration and:

    add_column :things, :tag_name, :string
    
    Thing.all.each do |thing| 
      if thing.tag
        thing.tag_name = thing.tag.name
      end
    end
    
    remove_column :things, :tag_id
    

    This is a quick and dirty example and I might have missed something. the key thing is to add the new column, move the association, remove the old column.

    There are other scenarios like the case where the model is involved in a many to many association or some polymorphic association, but the general approach would be similar in those cases.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

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.