【问题标题】:rake: Define length of string and integer colsrake:定义字符串和整数列的长度
【发布时间】:2014-12-11 13:55:14
【问题描述】:

我搜索了这个,但没有找到任何东西,所以这是我的问题:

如何在 rake create table 语句中设置字符串和整数的长度?

MySQL 示例:

create table table (
  id int primary key auto_increment,
  string1 varchar(30) not null,
  string2 varchar(50) not null,
  int tinyint(3) not null default '0'
)

当我只需要存储较少数量的字符时,我不想将所有这些列设置为 varchar(255) 或大整数。

http://guides.rubyonrails.org/migrations.html 没有写任何关于此的内容:/

我应该如何尝试?

【问题讨论】:

  • 文档guides.rubyonrails.org/migrations.html 对此进行了说明,请参见此处:limit Sets the maximum size of the string/text/binary/integer fields. 只需查找并阅读即可。
  • 也向你说声“谢谢”!

标签: mysql ruby-on-rails rails-migrations


【解决方案1】:

要创建一个表,迁移是要走的路。该文档很好地显示了how to set them up 和configure them。这可能有点让人不知所措,所以这是对你来说很重要的部分:

1 - 从终端创建迁移

运行rails g migration create_foo_model,你会在/db/migrations中找到新文件

2 - 根据需要设置模型

对于您的情况,我添加了选项 limit 和 default。

class CreateFooModelMigration < ActiveRecord::Migration
  create_table :foo do |t|
    t.string :string1, :limit => 30
    t.string :string2, :limit => 50
    t.integer :int1, :limit => 3, :default => 0
  end
end

然后你应该运行rake db:migrate,它将运行这段代码并设置你的表。

【讨论】:

  • 非常感谢,极限就是我需要的!
猜你喜欢
  • 1970-01-01
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-17
  • 1970-01-01
相关资源
最近更新 更多