Skip to content

Instantly share code, notes, and snippets.

@deepak
Created May 16, 2013 06:15
Show Gist options
  • Select an option

  • Save deepak/5589726 to your computer and use it in GitHub Desktop.

Select an option

Save deepak/5589726 to your computer and use it in GitHub Desktop.
limitless-strings-for-postgresql taken from Josh Susser's post at http://blog.hasmanythrough.com/2011/6/1/limitless-strings-for-postgresql
# TODO: add integration tests. to guard against rails updates and patches
# NOTE: http://blog.hasmanythrough.com/2011/6/1/limitless-strings-for-postgresql
# postgres extends the SQL standard to define "character" datatype
# without a limit.
# as per sql-standard character needs a limit.
# in postgres varchar is actually implemented as text with a limit
# constraint. so varchar might be a little slower than text.
# unlike mysql there is no performance penalty for text
# we can use text instead of string but some rails form generators
# will show a text-field for string and a text-area html element
# for text,
# from the postges manual:
# http://www.postgresql.org/docs/8.3/static/datatype-character.html
# Tip: There are no performance differences between these three
# types, apart from increased storage size when using the
# blank-padded type, and a few extra cycles to check the length
# when storing into a length-constrained column. While
# character(n) has performance advantages in some other database
# systems, it has no such advantages in PostgreSQL. In most
# situations text or character varying should be used instead.
initializer "postgresql.no_default_string_limit" do
ActiveSupport.on_load(:active_record) do
if defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:string].delete(:limit)
end
end
end
@deepak
Copy link
Copy Markdown
Author

deepak commented May 16, 2013

for some form builders text would render a textbox. so we want to use a string column and a textbox but do not care for a limit to be set on the string length

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment