How to change primary ID of a record in Rails?

后端 未结 6 1710
野的像风
野的像风 2020-12-28 16:10
rails console
u = User.find(9)
u.id = 7 # There is no other record with id 7
u.save
=> true
User.all

The id has not changed.

How to change the primary

相关标签:
6条回答
  • 2020-12-28 16:28

    The ID is typically generated by the database as an auto-incrementing PK. You can't (and really shouldn't need) to modify it.

    0 讨论(0)
  • 2020-12-28 16:30

    I'm not sure as to why it prevents that action, but it does for sure prevent it.

    You can bypass this using the update_all method on for the user.

    User.where(id: 7).update_all(id: 9)
    

    Though if possible, you really should avoid doing this.

    0 讨论(0)
  • 2020-12-28 16:32

    @Jason pointed out a very valid point. I totally agree with him. But, you might have your reasons and I suggest you re-consider what you're trying to do.

    To answer your question:

    ID columns are protected by default for mass assignment and cannot be set manually. But, you can override this behavior by defining a method:

    def self.attributes_protected_by_default
      [] # ["id", ..other]
    end
    

    This will allow you to assign id's manually.

    0 讨论(0)
  • 2020-12-28 16:42

    Another method (although it is not pure Rails) is to create a new column, and populate it with your new IDs.

    Then, using DB management software (not Rails), remove the Primary Key attribute from the id column, delete it, rename your recently added column to "id", and give it the Primary Key attributes. (If you cannot do that directly in your DB software, then set the properties Unique, Auto-Increment, etc.)

    You can also move the column to the front (MySQL syntax):

    ALTER TABLE table_name MODIFY COLUMN id int(11) FIRST;
    

    But there is another thing I'd really like to say. It hasn't been as bad on this question as I've seen elsewhere, but folks: it's all well and good to tell people it's USUALLY not a good idea, but that isn't an answer to the question. Please refrain from saying "Don't do that" unless you already know the person's use-case.

    In other forums I've been greatly frustrated by people saying "Why do you want to do that?" or "Don't do that", and then not answering the question. They didn't give me credit for already KNOWING that it isn't standard practice, and they ASSUMED I didn't already know that it was not an ordinary use-case.

    People on this page haven't been that bad, and I'm not trying to pick on them. I'm just admonishing: please, check your own behavior before presuming to lecture. Somebody asked a question, and while warnings may be a good idea, it is probably safe to presume they have a REASON for wanting an answer.

    End of rant.

    0 讨论(0)
  • 2020-12-28 16:52

    For me worked:

    User.find(9).update_column(:id, 7)
    
    0 讨论(0)
  • 2020-12-28 16:55

    Could you elaborate what your use case is? Why do you want to change the ID? What are you really trying to accomplish with it?

    Generally it's a bad idea to do this, and Rails won't let you do this easily because it will break your data integrity!

    Here's Why: When you're using a relational database (like PostgreSQL) underneath, you will have relationships between your models, which means that you will use the model's IDs as a reference in other related models... which means that if you change an entry's ID , all those references to that entry will go stale and corrupt your database..

    I would strongly suggest to analyze your requirements again, and try to find another way to accomplish what you need to do (without changing IDs)

    0 讨论(0)
提交回复
热议问题