更改主键发行Rails 4.0 [英] Change Primary Key Issue Rails 4.0

查看:82
本文介绍了更改主键发行Rails 4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了有关如何更改主键的指南:

I followed this guide on how to change the Primary Key:

这是我的代码:

class Pk2 < ActiveRecord::Migration
  def up
    remove_column :contracts, :id # remove existing primary key
    rename_column :contracts, :contractId, :id # rename existing UDID column
    execute "ALTER TABLE contracts ADD PRIMARY KEY (id);"
  end

  def down
    # Remove the UDID primary key. Note this would differ based on your database
    execute "ALTER TABLE contracts DROP CONSTRAINT table_pkey;"
    rename_column :contracts, :id, :contractId
    add_column :contracts, :id, :primary_key
  end
end

我一直收到的错误是围绕ALTER TABLE表ADD PRIMARY KEY的语法"

The error I keep getting is "Syntax around ALTER TABLE table ADD PRIMARY KEY"

请帮助.谢谢.

推荐答案

答案似乎是OP将迁移分为3个文件:

The answer seemed to be that the OP separated the migration into 3 files:

1. Remove `:id` column
2. Rename `:contractId` column to `:id`
3. run `execute "ALTER TABLE contracts ADD PRIMARY KEY (id);"`

这使OP可以成功运行迁移

This allowed the OP to successfully run the migration

其他一些资源:

  • Using Rails, how can I set my primary key to not be an integer-typed column?
  • Problems setting a custom primary key in a Rails 4 migration

这篇关于更改主键发行Rails 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆