How to create columns like "bigint" or "longtext" in Rails migrations
来源:互联网 发布:网络程序员教程 编辑:程序博客网 时间:2024/05/22 11:56
Rails understands a :limit
options when you create columns in a migration. Its meaning depends on the column type, and sometimes the supplied value.
The documentation states that :limit
sets the column length to the number of characters for string
and text
columns, and to the number of bytes for binary
and integer
columns.
Using it
This is nice since you may want a bigint
column to store really long numbers in it. You can just create it by saying integer, :limit => 8
and you'll be able to store 8 bytes of data. Similarly, you can use it to get a mediumtext
or longtext
column by applying the proper limits (65535 bytes would be a text
, up to 16777215 a mediumtext
, and up to 4294967295 a longtext
).
Or if you know that you'll never store more than a certain value, you might want to choose smaller column sizes for performance (it won't matter until your project becomes big, so don't worry about it for now).
Integer column lengths, mediumint
, bigint
, and all that other stuff
Mind that some values have a special meaning for integer
:
The first case is probably not surprising, it's just the default: when you say integer :my_column
, you just get an int
, or int(11)
since its maximum value has 11 digits. You'll get the same when saying :limit => 4
just because an int
holds up to 4 bytes of data.
However, mind that while you might think saying :limit => 11
gives you something like a superbigint
or at least an exception, it will in fact create an int
column, which is smaller than a bigint
.
These are values for MySQL. I don't know what happens on other database adapters
--------------------------------------------------------------------------------------------------------
Using BIGINT columns in rails migrations
Sometimes, you need to use BIGINT columns in your databases, e.g. when working with Twitter whose IDs are over 32 bits long now, and will be 64 bits long soon.
After some googling which didn’t bring up any good results, a quick look into the adapter code helps us:
# activerecord-3.0.0/lib/active_record/connection_adapters/mysql_adapter.rb# Maps logical Rails types to MySQL-specific data types.def type_to_sql(type, limit = nil, precision = nil, scale = nil) return super unless type.to_s == 'integer' case limit when 1; 'tinyint' when 2; 'smallint' when 3; 'mediumint' when nil, 4, 11; 'int(11)' # compatibility with MySQL default when 5..8; 'bigint' else raise(ActiveRecordError, "No integer type has byte size #{limit}") endend
So, use integer with :limit => 8
to get a bigint column. If you need a primary key of type bigint, you need to pass :id => false
to your create_table call, then define the id column manually:
class CreateDemo < ActiveRecord::Migration def self.up create_table :demo, :id => false do |t| t.integer :id, :limit => 8 end endend
- How to create columns like "bigint" or "longtext" in Rails migrations
- How to create readonly columns in MOSS 2007 custom list
- How to create an Excel sheet or chart in VC?
- How to create an Excel sheet or chart in VC?
- How To Create Your Own Personal Document Viewer (Like Scribd or Google Books)
- How to create function polymorphism in C
- How to Create Procedure in DB2
- How To Create Alternate Background In WPF?
- How to create Function/Procedure in Oracle?
- How to create a user in PostgreSQL
- How to create scheduler in liferay portlet
- How to Create QuickAction Dialog in Android
- How to: Create a Binding in Code
- How to create new module in npm
- How to create custom price in Magento?
- How To Create Database In MongoDB
- How to Create Mysite in SharePoint 2010
- How to create this array in Ruby?
- uva1424 saleman
- javabean出现找不到属性值的问题解决
- Mina框架研究(3)
- 第24题 Reverse Words in a String
- 将计算机思维故事化——之操作系统典型调度算法
- How to create columns like "bigint" or "longtext" in Rails migrations
- android面试感言
- Lua学习之【3】:表达式
- android学习笔记——linux环境下android开发环境的搭建
- BarberShop-java thread Synchronizing Demo
- linux线程私有数据
- 安卓项目开发实战(2)--星座展示12宫格
- 获取对固定列不重复的新DataTable
- UVA 11525Permutation