Rails 3/postgres - 如果不在架构中应用 :limit ,字符串有多长

发布于 2024-12-15 16:24:20 字数 111 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2024-12-22 16:24:20

如果您没有指定特定限制,ActiveRecord 将使用 varchar(255)(或迂腐的 character Varying (255))。您始终可以使用 psql 跳入 PostgreSQL 并输入 \d your_table 来获取 PostgreSQL 所看到的表。

我认为默认值没有在任何地方指定,但它是 就在这里来源

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

最接近规范的是迁移指南 :

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

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


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

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.

〆一缕阳光ご 2024-12-22 16:24:20

在 Rails 4 中,字符串类型没有默认限制,如您在 来源

NATIVE_DATABASE_TYPES = {
        primary_key: "serial primary key",
        bigserial: "bigserial",
        string:      { name: "character varying" },
        text:        { name: "text" },
        #...

如果您没有指定限制,ActiveRecord 只会设置字符变化,并且您可以存储任意长度的字符串,如 文档

如果在没有长度说明符的情况下使用字符变化,则该类型接受任何大小的字符串

In rails 4 there is no default limit for string type as you can see in the source:

NATIVE_DATABASE_TYPES = {
        primary_key: "serial primary key",
        bigserial: "bigserial",
        string:      { name: "character varying" },
        text:        { name: "text" },
        #...

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:

If character varying is used without length specifier, the type accepts strings of any size

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文