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

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

问题描述

如何为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选项传递给

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"

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

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