Rails:用于创建固定长度char(12)列的迁移

时间:2022-09-26 21:38:22

What is the best way to define a fixed-length SQL column (CHAR(12) for instance) through a Rails migration ?

通过Rails迁移定义固定长度SQL列(例如CHAR(12))的最佳方法是什么?

Why this should not handled by the model is because of the performance of char() vs varchar(), and I'd like to avoid injecting raw SQL in the database.

为什么不应该由模型处理这是因为char()vs varchar()的性能,我想避免在数据库中注入原始SQL。

Edit : I know the :limit modifier, however the field is still varchar (which is bad for performance) and does not allow a minimum size.

编辑:我知道:limit修饰符,但是该字段仍然是varchar(这对性能不利)并且不允许最小大小。

4 个解决方案

#1


46  

If Rails doesn’t understand the column type, it’ll pass it straight through to the database. So if you want a char instead of varchar, just replace:

如果Rails不理解列类型,它会直接将其传递给数据库。因此,如果您想要一个char而不是varchar,只需替换:

t.column :token, :string

With:

附:

t.column :token, "char(12)"

Of course, this may or may not make your migrations non-portable to another database.

当然,这可能会也可能不会使您的迁移不可移植到另一个数据库。

(credit to http://laurelfan.com/2010/1/26/special-mysql-types-in-rails-migrations)

(信用到http://laurelfan.com/2010/1/26/special-mysql-types-in-rails-migrations)

#2


25  

 def self.up
    add_column("admin_users", "username", :string, :limit => 25)
 end

 def self.down
    remove_column("admin_users", "username")
 end

#3


9  

You can use string type with limit option in your migration file like this:

您可以在迁移文件中使用字符串类型和限制选项,如下所示:

t.string :name, :limit => 12, :null => false

#4


4  

For a database specific type, we can now use:

对于特定于数据库的类型,我们现在可以使用:

t.column(:column_name, 'char(12)')

And for a complete example:

并举一个完整的例子:

class Foo < ActiveRecord::Migration
  def change
     create_table :foo do |t|
       t.column(:column_name, 'custom_type')

       t.timestamps
     end
  end
end

#1


46  

If Rails doesn’t understand the column type, it’ll pass it straight through to the database. So if you want a char instead of varchar, just replace:

如果Rails不理解列类型,它会直接将其传递给数据库。因此,如果您想要一个char而不是varchar,只需替换:

t.column :token, :string

With:

附:

t.column :token, "char(12)"

Of course, this may or may not make your migrations non-portable to another database.

当然,这可能会也可能不会使您的迁移不可移植到另一个数据库。

(credit to http://laurelfan.com/2010/1/26/special-mysql-types-in-rails-migrations)

(信用到http://laurelfan.com/2010/1/26/special-mysql-types-in-rails-migrations)

#2


25  

 def self.up
    add_column("admin_users", "username", :string, :limit => 25)
 end

 def self.down
    remove_column("admin_users", "username")
 end

#3


9  

You can use string type with limit option in your migration file like this:

您可以在迁移文件中使用字符串类型和限制选项,如下所示:

t.string :name, :limit => 12, :null => false

#4


4  

For a database specific type, we can now use:

对于特定于数据库的类型,我们现在可以使用:

t.column(:column_name, 'char(12)')

And for a complete example:

并举一个完整的例子:

class Foo < ActiveRecord::Migration
  def change
     create_table :foo do |t|
       t.column(:column_name, 'custom_type')

       t.timestamps
     end
  end
end