Mysql rake:定义字符串长度和整数列
我搜索了这个,但什么也没找到,所以我的问题是: 如何在rake create table语句中设置字符串和整数的长度 示例MySQL:Mysql rake:定义字符串长度和整数列,mysql,ruby-on-rails,rails-migrations,Mysql,Ruby On Rails,Rails Migrations,我搜索了这个,但什么也没找到,所以我的问题是: 如何在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' ) 当我只需要存储较短数量的字符时,我不想
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)或大整数
没有写任何关于此的内容:/
我应该如何尝试这一点?要创建一个表,迁移是必由之路。文档很好地显示了&。这可能有点让人难以承受,所以这里有一个重要的部分: 1-从终端创建迁移 运行
railsgmigration create\u foo\u 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
class CreateFoodModelMigration30
t、 字符串:string2,:limit=>50
t、 整数:int1,:limit=>3,:default=>0
结束
结束
然后您应该运行
rake db:migrate
,它将运行此代码并因此设置您的表。文档对此进行了说明,请参见此处:limit设置字符串/文本/二进制/整数字段的最大大小。
只需查找并阅读即可。谢谢!非常感谢,限额是我需要的!