【问题标题】:How can I increase a field in a rails gem?如何增加rails gem中的字段?
【发布时间】:2013-03-04 03:47:38
【问题描述】:

我收到此错误:

 SQL (31.1ms)  INSERT INTO "read_marks" ("readable_id", "readable_type", "timestamp", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id"  [["readable_id", nil], ["readable_type", "PublicActivity::ORM::ActiveRecord::Activity"], ["timestamp", Mon, 04 Mar 2013 03:29:52 UTC +00:00], ["user_id", 2]]
PG::Error: ERROR:  value too long for type character varying(20)

因为“可读类型”仅包含 20 个字符,而我正在传递“PublicActivity::ORM::ActiveRecord::Activity”。

这与using gem unread with public_activity 的问题相同(他显然解决了问题,但没有说明如何解决(见他的底部更新))

【问题讨论】:

    标签: ruby-on-rails notifications gem rails-postgresql


    【解决方案1】:

    为什么不进行迁移并将readable_type 更改为更长的类型?像这样的:

    change_column :read_marks, :readable_type, "varchar(255)"
    

    或者如果你想走得更久:

    change_column :read_marks, :readable_type, :text
    

    由于您使用的是 PostgreSQL,因此使用“无限”长度类型没有任何缺点,例如:text.

    【讨论】:

    • 我当时想,“不可能这么简单”,然后就是
    【解决方案2】:

    来自您添加的链接:

    UPDATE
    
    AH! It was the readable_type field having a limit of only 20 that was truncating my class name. Now it works after I increased the size.
    

    在数据库上,将“可读类型”列的大小增加到大于 20 的大小

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-10
      • 2016-10-25
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      相关资源
      最近更新 更多