質問

How do i do this? My original schema I had a table called "users" that had a bunch of records. I deleted my original migration by accident and had to create a new one except the new one has a new additional record prior to the old one.

Sorry Im new to rails and databases specifically. Help would be greatly appreciated

役に立ちましたか?

解決

Maybe this will help you out.

Some Database Terminology

  • A database has schemas, tables, and records (upon other things).

  • A schema is the structure of the database. Things like table names, column names, and relationships.

  • A database has tables.

  • A table has records.

  • A record is a row of data in a table.

Migrations

A migration modifies the schema, or structure of the database, in some way. For example, creating a new table or adding a column to a table. Migrations are beneficial because you can keep track to the changes you make to the schema or even "undo" one.

An Example

Let's say we already have a users table but we want to add a column called 'full_name' to it. We can create a migration:

$ rails generate migration AddFullNameToUsers full_name:string

This will make a file in db/migrate called <date>_add_full_name_to_users.rb containing:

class AddFullNameToUsers < ActiveRecord::Migration
  def change
    add_column :users, :full_name, :string
  end
end

However, no changes have been made to the database yet! We need to execute the migration:

$ rake db:migrate

Now our users table has a new column called full_name. If we want to undo that we can rollback the migration:

$ rake db:rollback

Our user table no longer has a full_name column.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top