【问题标题】:smallint is not working to genrate model and table rails 3+ postgreSQLsmallint 无法生成模型和表 rails 3+ postgreSQL
【发布时间】:2014-09-02 19:54:22
【问题描述】:

首先我运行了这个命令

rails generate model FeedbackComment type:smallint reply:text

之后

rake db:migrate 

我收到这个错误

StandardError: An error has occurred, this and all later migrations canceled:

undefined method `smallint' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition:0x9d1a318>/var/www/blog/db/migrate/20140712064127_create_feedback_comments.rb:4:in `block in change'

如何在 postgreSQL 中通过命令创建 smallint?

请帮帮我

【问题讨论】:

  • 没有支持Railssmallint数据类型。给integer数据类型limit
  • 像这样的rails generate model FeedbackComment type:integer{2} reply:text.2 bytes of integersmallint
  • 你有什么错误吗?
  • SyntaxError: /var/www/blog/db/migrate/20140712071128_create_users.rb:4: 语法错误,意外':',期待keyword_end t.smallint{2} :reply_type
  • 我说像这样给rails generate model FeedbackComment type:integer{2} reply:textinteger 不是 smallint

标签: ruby-on-rails postgresql mode


【解决方案1】:

正如我所说,没有支持 Rails 3smallint。您应该使用integer datatypelimit of 2 bytes 使其成为smallint

有关可用 Rails 3 数据类型的列表,请参阅此 SO 帖子。

这个命令会给你你想要的

rails generate model FeedbackComment type:integer{2} reply:text

查看此 link 了解高级 Rails 模型生成器。

这里有一些更有用的信息

:limit     Numeric Type  Column Size    Max value
1          tinyint       1 byte         127
2          smallint      2 bytes        32767
3          mediumint     3 bytes        8388607
nil, 4, 11 int(11)       4 bytes        2147483647
5..8       bigint        8 bytes        9223372036854775807

【讨论】:

    【解决方案2】:

    在Rails 6、5、4中添加PostgreSQLsmallint数据类型的方式还是一样的,一个整数数据类型,限制为2。

    class AddColumnNameToTableName < ActiveRecord::Migration[6.0]
      def change
        add_column :table_name, :column_name, :integer, limit: 2
      end
    end
    

    # =&gt; column_name smallint.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多