Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an ActiveRecord database table with no :id column?

Is it possible for me to create and use a database table that contains no :id column in ActiveRecord, Ruby on Rails.

I don't merely want to ignore the id column, but I wish it to be absolutely non-existent.

Table Example  :key_column                         :value_column 0cc175b9c0f1b6a831c399e269772661    0cc175b9c0f1b6a831c399e269772661 4a8a08f09d37b73795649038408b5f33    0d61f8370cad1d412f80b84d143e1257 92eb5ffee6ae2fec3ad71c777531578f    9d5ed678fe57bcca610140957afab571 

Any more info ( like an :id_column ) would break the whole feature.

How would I implement something like this in rails?

like image 708
Stefan Avatar asked May 17 '09 13:05

Stefan


People also ask

How do I add a reference column in Rails?

When you already have users and uploads tables and wish to add a new relationship between them. Then, run the migration using rake db:migrate . This migration will take care of adding a new column named user_id to uploads table (referencing id column in users table), PLUS it will also add an index on the new column.

Can you use Active Record without Rails?

You don't need a Rails app directory or a SQLite database file; all you need is these two Ruby code files.

How do you delete a column in Rails?

How do I remove a column from a table in Ruby on Rails? remove_column :table_name, :column_name, :type Removes column, also adds column back if migration is rollbacked. Note: If you skip the data_type, the migration will remove the column successfully but if you rollback the migration it will throw an error.


1 Answers

yup, looks like this:

create_table :my_table, id: false do |t|   t.string :key_column   t.string :value_column end 

just make sure to include the

id: false 

part.

like image 80
matt Avatar answered Sep 16 '22 22:09

matt