How to change primary key in rails migration file?

后端 未结 2 443
孤独总比滥情好
孤独总比滥情好 2021-02-08 13:29

I need to migrate an old mysql table like this:

Products
  name (string, primary_key)

to this schema:

Products
  id (integer, p         


        
相关标签:
2条回答
  • 2021-02-08 14:10

    If you're on Postgresql, the syntax is slightly different.

    ALTER TABLE <table_name> DROP CONSTRAINT <table_name>_pkey;
    
    0 讨论(0)
  • 2021-02-08 14:19

    You could execute arbitrary SQL in your migration:

    execute "ALTER TABLE `products` DROP PRIMARY KEY"
    

    and then add the new column:

    add_column :products, :id, :primary_key
    

    See:

    Remove Primary Key in MySQL

    how to add a primary key to a table in rails

    http://thinkwhere.wordpress.com/2009/05/09/adding-a-primary-key-id-to-table-in-rails/

    http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

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