Skip to content

Commit

Permalink
Postgresql doesn't accept limits on text columns.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwnall committed Nov 20, 2012
1 parent bd87bd9 commit f307cbf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 3.2.10 (unreleased) ## Rails 3.2.10 (unreleased)


* When running migrations on Postgresql, the `:limit` option for `binary` and `text` columns is silently dropped.
Previously, these migrations caused sql exceptions, because Postgresql doesn't support limits on these types.

*Victor Costan*

* Calling `include?` on `has_many` associations on unsaved records no longer * Calling `include?` on `has_many` associations on unsaved records no longer
returns `true` when passed a record with a `nil` foreign key. returns `true` when passed a record with a `nil` foreign key.
Fixes #7950. Fixes #7950.
Expand Down
Expand Up @@ -1078,6 +1078,13 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil)
when nil, 0..0x3fffffff; super(type) when nil, 0..0x3fffffff; super(type)
else raise(ActiveRecordError, "No binary type has byte size #{limit}.") else raise(ActiveRecordError, "No binary type has byte size #{limit}.")
end end
when 'text'
# PostgreSQL doesn't support limits on text columns.
# The hard limit is 1Gb, according to section 8.3 in the manual.
case limit
when nil, 0..0x3fffffff; super(type)
else raise(ActiveRecordError, "The limit on text can be at most 1GB - 1byte.")
end
when 'integer' when 'integer'
return 'integer' unless limit return 'integer' unless limit


Expand Down
18 changes: 18 additions & 0 deletions activerecord/test/cases/adapters/postgresql/sql_types_test.rb
@@ -0,0 +1,18 @@
require "cases/helper"

class SqlTypesTest < ActiveRecord::TestCase
def test_binary_types
assert_equal 'bytea', type_to_sql(:binary, 100_000)
assert_raise ActiveRecord::ActiveRecordError do
type_to_sql :binary, 4294967295
end
assert_equal 'text', type_to_sql(:text, 100_000)
assert_raise ActiveRecord::ActiveRecordError do
type_to_sql :text, 4294967295
end
end

def type_to_sql(*args)
ActiveRecord::Base.connection.type_to_sql(*args)
end
end
5 changes: 5 additions & 0 deletions activerecord/test/schema/postgresql_specific_schema.rb
Expand Up @@ -132,5 +132,10 @@
_SQL _SQL
rescue #This version of PostgreSQL either has no XML support or is was not compiled with XML support: skipping table rescue #This version of PostgreSQL either has no XML support or is was not compiled with XML support: skipping table
end end

create_table :limitless_fields, force: true do |t|
t.binary :binary, limit: 100_000
t.text :text, limit: 100_000
end
end end


0 comments on commit f307cbf

Please sign in to comment.