如何在 ruby​​ on rails 迁移中设置主键? [英] How to set primary key in ruby on rails migration?

查看:27
本文介绍了如何在 ruby​​ on rails 迁移中设置主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置 IdClient 字段的主键?我已经尝试了所有方法,但我会得到错误(rails 3.0.9)...你能帮帮我吗?

How can I set primary key for my IdClient field? I have tried all methods, but I'll get errors (rails 3.0.9)... Could you help me?

class CreateCustomers < ActiveRecord::Migration
  def self.up
    create_table :customers do |t|
      t.integer :IdCustomer
      t.string :username
      t.string :crypted_password
      t.string :password_salt
      t.string :persistence_token
      t.string :email
      t.string :Skype
      t.string :ICQ
      t.string :Firstname
      t.string :Lastname
      t.string :Country
      t.string :State
      t.string :City
      t.string :Street
      t.string :Building
      t.integer :Room
      t.string :AddressNote
      t.date :DateOfReg
      t.integer :CustGroup
      t.float :TotalBuy

      t.timestamps
      add_index(:customers, :IdCustomer, :unique => true)
    end
  end

  def self.down
    drop_table :customers
  end
end

还有如何在模型中设置关系?

Also how to set relations in model?

推荐答案

不要这样做.使用内置的 id 字段作为主键.如果您打算使用 Rails,您应该以Rails 方式"构建您的应用程序,除非您有非常充分的理由不这样做.

Don't do this. Use the built-in id field as the primary key. If you're going to use Rails, you should build your app the "Rails way" unless you have very good reason not to.

如果您真的想这样做,可以将 :primary_key 选项传递给 create_table:

If you really want to do this, you can pass a :primary_key option to create_table:

create_table :customers, :primary_key => :idClient do |t|
  # ...
end

您还需要通过 self.primary_key = "idClient"

这篇关于如何在 ruby​​ on rails 迁移中设置主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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