Rails 3/postgres - 如果不在架构中应用 :limit ,字符串有多长
我的 googlefu 一定很弱,因为我找不到任何东西来告诉我 Rails 应用程序(托管在 Heroku,使用 PostgreSQL 作为数据库)中字符串列的默认限制。
任何帮助将不胜感激!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您没有指定特定限制,ActiveRecord 将使用
varchar(255)
(或迂腐的character Varying (255)
)。您始终可以使用psql
跳入 PostgreSQL 并输入\d your_table
来获取 PostgreSQL 所看到的表。我认为默认值没有在任何地方指定,但它是 就在这里来源:
最接近规范的是迁移指南 :
但这与 PostgreSQL 无关,也不完全是一个保证。
顺便说一句,如果您使用 PostgreSQL,您几乎应该总是直接转到
:text
并假装:string
不存在。 PostgreSQL 在内部对它们的处理方式相同,只是它必须对varchar
进行长度检查。我的另一个答案对此有更多讨论:将列类型更改为 Rails 中的较长字符串。ActiveRecord uses
varchar(255)
(orcharacter varying (255)
to be pedantic) if you don't specify a specific limit. You can always hop into PostgreSQL withpsql
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:
The closest thing to a specification is in the Migrations Guide:
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 onvarchar
. 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 4 中,字符串类型没有默认限制,如您在 来源:
如果您没有指定限制,ActiveRecord 只会设置
字符变化
,并且您可以存储任意长度的字符串,如 文档:In rails 4 there is no default limit for string type as you can see in the source:
if you don't specify a limit ActiveRecord will just set
character varying
and you could store there a string of any length as stated in the documentation: