Skip to content

Commit 5d6cca1

Browse files
committed
Merge pull request rails#6418 from pwnall/pgsql_bytea_limit3
Postgresql doesn't accept limits on binary (bytea) columns (for 3-2-stable)
2 parents f051071 + 36fdb72 commit 5d6cca1

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,14 +1067,25 @@ def index_name_length
10671067

10681068
# Maps logical Rails types to PostgreSQL-specific data types.
10691069
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
1070-
return super unless type.to_s == 'integer'
1071-
return 'integer' unless limit
1072-
1073-
case limit
1074-
when 1, 2; 'smallint'
1075-
when 3, 4; 'integer'
1076-
when 5..8; 'bigint'
1077-
else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
1070+
case type.to_s
1071+
when 'binary'
1072+
# PostgreSQL doesn't support limits on binary (bytea) columns.
1073+
# The hard limit is 1Gb, because of a 32-bit size field, and TOAST.
1074+
case limit
1075+
when nil, 0..0x3fffffff; super(type)
1076+
else raise(ActiveRecordError, "No binary type has byte size #{limit}.")
1077+
end
1078+
when 'integer'
1079+
return 'integer' unless limit
1080+
1081+
case limit
1082+
when 1, 2; 'smallint'
1083+
when 3, 4; 'integer'
1084+
when 5..8; 'bigint'
1085+
else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
1086+
end
1087+
else
1088+
super
10781089
end
10791090
end
10801091

activerecord/test/schema/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def create_table(*args, &block)
7575
create_table :binaries, :force => true do |t|
7676
t.string :name
7777
t.binary :data
78+
t.binary :short_data, :limit => 2048
7879
end
7980

8081
create_table :birds, :force => true do |t|

0 commit comments

Comments
 (0)