I have the following ActiveRecord migration:
class CreateSubjects < ActiveRecord::Migration
def self.up
create_table :subjects do |t|
t.string :title
t.timestamps
end
change_table :projects do |t|
t.references :subjects
end
end
def self.down
drop_table :subjects
remove_column :projects, :subjects_id #defeats the purpose of having references
end
end
I actually like the references
style. Unfortunately I could not find the rollback equivalent of references
in the self.down
section. If I write remove_column :projects, :subjects_id
I can as well write t.integer :subjects_id
, which would make it safer.
Definition of roll back (Entry 2 of 2) transitive verb. 1 : to reduce (something, such as a commodity price) to or toward a previous level on a national scale. 2 : to cause to retreat or withdraw : push back. 3 : rescind attempted to roll back antipollution standards.
You can use ROLLBACK TRANSACTION to erase all data modifications made from the start of the transaction or to a savepoint. It also frees resources held by the transaction. This does not include changes made to local variables or table variables. These are not erased by this statement.
ROLLBACK in SQL is a transactional control language that is used to undo the transactions that have not been saved in the database. The command is only been used to undo changes since the last COMMIT. Example: Consider the following STAFF table with records: STAFF.
ROLLBACK is the SQL command that is used for reverting changes performed by a transaction. When a ROLLBACK command is issued it reverts all the changes since last COMMIT or ROLLBACK.
It is called remove_references.
t.remove_references :subjects
Be careful! Rails uses singular by convention, should be:
def self.up
create_table :subjects do |t|
t.string :title
t.timestamps
end
change_table :projects do |t|
t.references :subject
end
end
def self.down
drop_table :subjects
change_table :projects do |t|
t.remove_references :subject
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With