加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 20:56:51 所属栏目:百科 来源:网络整理
导读:如何为我的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 :cr
如何为我的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”告诉您的模型其主键的名称

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读