Rails3 bigint primary key

I would like to create bigint (or string or something else, not int )), the typed primary key field under Rails 3.

I have a given data structure, for example:

 things ------ id bigint primary_key name char(32) 

The approach I'm trying to click is:

 create_table :things, :id => false do |t| # That prevents the creation of (id int) PK t.integer :id, :limit => 8 # That makes the column type bigint t.string :name, :limit => 32 t.primary_key :id # This is perfectly ignored :-( end 

The column type will be correct, but the primary key parameter will not be present in sqlite3, and I suspect this also applies to MySQL.

+8
ruby-on-rails ruby-on-rails-3 bigint primary-key
source share
5 answers

Not so long ago, and found the answer here: Using Rails, how can I set my primary key so that it is not a column with an integer type?

You need to set primary_key: false, and then use the user-defined operator before performing the migration.

EDIT 1: You need to check the database documents for an exact query. It runs as a regular SQL query and must be database specific. The example in the question I talked about relates to Postgre SQL. If you are using MySQL, you may need to change this.

+3
source share

I had the same problem. I think the easiest way for the table

 accounts id bigint primary key name char 

is an

 create_table :accounts do |t| t.string :name end change_column :accounts, :id , "bigint NOT NULL AUTO_INCREMENT" 
+4
source share

For MySQL, you can use "SERIAL", which is an alias for "BIGINT UNSIGNED NOT NULL AUTO_INCREMENT"

 class ChangeUserIdToBigint < ActiveRecord::Migration def change change_column :users, :id, 'SERIAL' end end 
+3
source share

skalogirou's answer is good, but the change will not be reflected in schema.rb. Thus, tasks like db:schema:load and db:test:clone will not create an identical database structure.

A necessary workaround is to increase db:schema:load and db: test: clone rake tasks, as described here: http://www.lshift.net/blog/2013/09/30/changing-the-primary-key-type -in-ruby-on-rails-models /

This is what I used based on this workaround:

 namespace :my_app do namespace :db do task :after_schema_load_and_db_test_clone => :environment do puts 'Changing primary key for :my_table' query = 'ALTER TABLE <my_table> CHANGE id id bigint DEFAULT NULL auto_increment' ActiveRecord::Base.connection.execute(query) end end Rake::Task['db:schema:load'].enhance do ::Rake::Task['my_app:db:after_schema_load_and_db_test_clone'].invoke end Rake::Task['db:test:clone'].enhance do ::Rake::Task['my_app:db:after_schema_load_and_db_test_clone'].invoke end 
0
source share

If you want to convert all tables to Postgres, you will need to run this code

 class ConvertIntToBigint < ActiveRecord::Migration[5.1] def up query = <<-SQL SELECT tablename AS "tablename" FROM pg_tables WHERE schemaname = 'public'; SQL connection.execute(query).each do |element| if column_exists?(element['tablename'], :id, :integer) change_table(element['tablename']) {|t| t.change :id, :bigint } end end end def down end end 
0
source share

All Articles