Skip to content

Commit

Permalink
Postgresql doesn't accept limits on binary (bytea) columns.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwnall committed May 21, 2012
1 parent b0f8355 commit 36fdb72
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Expand Up @@ -1067,14 +1067,25 @@ def index_name_length

# Maps logical Rails types to PostgreSQL-specific data types.
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
return super unless type.to_s == 'integer'
return 'integer' unless limit

case limit
when 1, 2; 'smallint'
when 3, 4; 'integer'
when 5..8; 'bigint'
else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
case type.to_s
when 'binary'
# PostgreSQL doesn't support limits on binary (bytea) columns.
# The hard limit is 1Gb, because of a 32-bit size field, and TOAST.
case limit
when nil, 0..0x3fffffff; super(type)
else raise(ActiveRecordError, "No binary type has byte size #{limit}.")
end
when 'integer'
return 'integer' unless limit

case limit
when 1, 2; 'smallint'
when 3, 4; 'integer'
when 5..8; 'bigint'
else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
end
else
super
end
end

Expand Down
1 change: 1 addition & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -75,6 +75,7 @@ def create_table(*args, &block)
create_table :binaries, :force => true do |t|
t.string :name
t.binary :data
t.binary :short_data, :limit => 2048
end

create_table :birds, :force => true do |t|
Expand Down

0 comments on commit 36fdb72

Please sign in to comment.