微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – 如何在rails迁移时在ruby中设置主键?

如何为我的IdClient字段设置主键?我已经尝试了所有方法,但我会得到错误(rails 3.0.9)…你能帮助我吗?
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

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

解决方法

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

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

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

您还需要通过self.primary_key =“idClient”告诉您的模型其主键的名称

原文地址:https://www.jb51.cc/ruby/274345.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐