Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

smallint is not working to genrate model and table rails 3+ postgreSQL

first I have run this command

rails generate model FeedbackComment type:smallint reply:text

after then

rake db:migrate 

I am getting this error

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'

How can i create smallint through command in postgreSQL ?

Please hellp me

like image 716
Harman Avatar asked Jul 12 '14 06:07

Harman


2 Answers

As I said,there is no smallint that supports Rails 3.You should be using integer datatype with limit of 2 bytes to make it as smallint.

For a list of available Rails 3 datatypes,see this SO Post.

This command will give you what you want

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

See this link for advanced Rails model generators.

Here is some more useful info

: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
like image 66
Pavan Avatar answered Jan 04 '23 06:01

Pavan


The way of adding PostgreSQL smallint data type in Rails 6, 5, and 4 is still the same, an integer data type with limit 2.

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

# => column_name smallint.

like image 27
Ivica Lakatoš Avatar answered Jan 04 '23 05:01

Ivica Lakatoš