rails 3 / postgres-如果不应用,则字符串多长时间 [英] rails 3/postgres - how long is a string if you don't apply :limit in schema

查看:80
本文介绍了rails 3 / postgres-如果不应用,则字符串多长时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的googlefu必须很弱,因为我找不到任何东西可以告诉我Rails应用程序(托管在Heroku中,使用PostgreSQL作为数据库)中字符串列的默认限制。



任何帮助将不胜感激!

解决方案

ActiveRecord使用 varchar(255)(或字符变化(255)为书呆子),如果您未指定具体限制。您总是可以使用 psql 跳到PostgreSQL,然后说 \y your_table 来获取PostgreSQL看到的表。 / p>

我不认为默认值是在任何地方指定的,但是它是就在源代码中

  NATIVE_DATABASE_TYPES = {
:primary_key => 串行主键,
:字符串=> {:name => 字符变化,:limit => 255},
#...

最接近规格的是< a href = http://guides.rubyonrails.org/migrations.html#supported-types rel = noreferrer>迁移指南:


这些将被映射到适当的基础数据库类型,例如与MySQL :string 映射到 VARCHAR(255)


但这与PostgreSQL无关,也不完全是保证。






顺便说一句,如果您使用的是PostgreSQL,几乎应该总是直接使用:text 并假设:string 不存在。 PostgreSQL在内部对它们的处理相同,只是它必须对 varchar 进行长度检查。我的另一个答案是关于此的更多讨论: 将列类型更改为Rails中更长的字符串


My googlefu must be weak because I cannot find anything to tell me the default limit of a string column in my Rails app (hosted at Heroku, using PostgreSQL as the database).

Any help would be appreciated!

解决方案

ActiveRecord uses varchar(255) (or character varying (255) to be pedantic) if you don't specify a specific limit. You can always hop into PostgreSQL with psql and say \d your_table to get the table as PostgreSQL sees it.

I don't think the default is specified anywhere but it is right here in the source:

NATIVE_DATABASE_TYPES = {
  :primary_key => "serial primary key",
  :string      => { :name => "character varying", :limit => 255 },
  #...

The closest thing to a specification is in the Migrations Guide:

These will be mapped onto an appropriate underlying database type, for example with MySQL :string is mapped to VARCHAR(255).

But that's not about PostgreSQL and not exactly a guarantee.


As an aside, if you're using PostgreSQL, you should almost always go straight to :text and pretend that :string doesn't exist. PostgreSQL treats them the same internally except that it has to do a length check on varchar. There's a bit more discussion on this over here in another one of my answers: Changing a column type to longer strings in rails.

这篇关于rails 3 / postgres-如果不应用,则字符串多长时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆