How can I set the starting point for the primary key (ID) column in Postgres via a rails migration
Have no idea about rubies and railroads part, but query you're talking about is
ALTER SEQUENCE reports_something_seq RESTART 1000;
You will have to look up your table for the sequence name and postgresql documentation for general education regarding the matter ;-)
Add primary key to an existing field in Rails
I ended up doing this:
execute('ALTER TABLE inventories ADD PRIMARY KEY (id);')
Using Rails, how can I set my primary key to not be an integer-typed column?
Unfortunately, I've determined it's not possible to do it without using execute
.
Why it doesn't work
By examining the ActiveRecord source, we can find the code for create_table
:
In schema_statements.rb
:
def create_table(table_name, options={})
...
table_definition.primary_key(options[:primary_key] || Base.get_primary_key(table_name.to_s.singularize)) unless options[:id] == false
...
end
So we can see that when you try to specify a primary key in the create_table
options, it creates a primary key with that specified name (or, if none is specified, id
). It does this by calling the same method you can use inside a table definition block: primary_key
.
In schema_statements.rb
:
def primary_key(name)
column(name, :primary_key)
end
This just creates a column with the specified name of type :primary_key
. This is set to the following in the standard database adapters:
PostgreSQL: "serial primary key"
MySQL: "int(11) DEFAULT NULL auto_increment PRIMARY KEY"
SQLite: "INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL"
The workaround
Since we're stuck with these as the primary key types, we have to use execute
to create a primary key that is not an integer (PostgreSQL's serial
is an integer using a sequence):
create_table :employees, {:id => false} do |t|
t.string :emp_id
t.string :first_name
t.string :last_name
end
execute "ALTER TABLE employees ADD PRIMARY KEY (emp_id);"
And as Sean McCleary mentioned, your ActiveRecord model should set the primary key using set_primary_key
:
class Employee < ActiveRecord::Base
set_primary_key :emp_id
...
end
Insert sequence value in a column within the scope of a foreign key - Rails and Postgres
There is a gem called sequenced. I thought it will solve your problem.
Sequenced is a simple gem that generates scoped sequential IDs for
ActiveRecord models. This gem provides an acts_as_sequenced macro that
automatically assigns a unique, sequential ID to each record. The
sequential ID is not a replacement for the database primary key, but
rather adds another way to retrieve the object without exposing the
primary key.
The gem provides a Ruby/Rails implementation. It used ActiveModel::Callbacks to set the value of sequential_id
column: before_save :set_sequential_ids
[link].
def acts_as_sequenced(options = {})
# ...
include Sequenced::ActsAsSequenced::InstanceMethods
mattr_accessor :sequenced_options, instance_accessor: false
self.sequenced_options = []
before_save :set_sequential_ids
# ...
end
Here is the setter method [link]:
def set
return if id_set? || skip?
lock_table
record.send(:"#{column}=", next_id)
end
How to change primary key in rails migration file?
You could execute arbitrary SQL in your migration:
execute "ALTER TABLE `products` DROP PRIMARY KEY"
and then add the new column:
add_column :products, :id, :primary_key
See:
Remove Primary Key in MySQL
how to add a primary key to a table in rails
http://thinkwhere.wordpress.com/2009/05/09/adding-a-primary-key-id-to-table-in-rails/
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
Related Topics
Oracle SQL "Select Date from Datetime Field "
Get The Difference Between Two Dates Both in Months and Days in Sql
How to Query Database Name in Oracle SQL Developer
Field Value Must Be Unique Unless It Is Null
How Gremlin Query Same SQL Like for Search Feature
Sql Join Using a Mapping Table
Selecting Multiple Rows by Id, Is There a Faster Way Than Where In
How to Get a Real Time Within Postgresql Transaction
Using Sqldf and Rpostgresql Together
Sql Query to Find The Duplicate Records
How to Identify Views with Broken Dependencies in SQL Server
Does "Select for Update" Prevent Other Connections Inserting When the Row Is Not Present
Authoritative SQL Standard Documentation
Get The Last Modified Date for All Bigquery Tables in a Bigquery Project
Extract Time Part from Timestamp Column in Oracle
How to Update Rows of Two Tables That Have Foreign Key Restrictions