How to use long id in Rails applications?

后端 未结 11 1524
天涯浪人
天涯浪人 2020-11-30 22:24

How can I change the (default) type for ActiveRecord\'s IDs? int is not long enough, I would prefer long. I was surprised that there is no :long for the migrations - does on

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 23:18

    In rails4, you can do it.

    Following is an example to create a Dummy model in rails4 & postgres,

    xxx_migrate_dummies.rb:

    class CreateDummies < ActiveRecord::Migration
      def change
        create_table :dummies, :id => false do |t|
          t.column :id, :serial8, primary_key: true
          t.string :name, :limit => 50, null: false
          t.integer :size, null: false
    
          t.column :create_date, :timestamptz, null: false
        end
      end
    end
    

    What it did:

    • It use serial8 as id type, which is 64 bit integer, and define it as primary key.
    • It use timestamptz as datetime type, which contain the timezone info, this is important for a application that go across multiple timezones.

提交回复
热议问题