Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the corresponding rollback?

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.

like image 710
sebastiangeiger Avatar asked May 05 '11 10:05

sebastiangeiger


People also ask

What is rollback explain?

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.

What does rollback mean in transaction?

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.

What is rollback in SQL with example?

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.

What is rollback for in SQL?

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.


1 Answers

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
like image 114
Jan Minárik Avatar answered Oct 04 '22 09:10

Jan Minárik