Active Record Migrations - Ruby on Rails Guides - 0 views
-
-
張 旭 on 12 Apr 16跟 belongs_to 與 has_many 設定對應的 Migrattion
-
-
- ...114 more annotations...
-
Active Record will also update your db/schema.rb file to match the up-to-date structure of your database.
-
A primary key column called id will also be added implicitly, as it's the default primary key for all Active Record models
-
On databases that support transactions with statements that change the schema, migrations are wrapped in a transaction
-
change_table is also reversible, as long as the block does not call change, change_default or remove.
-
add_column add_foreign_key add_index add_reference add_timestamps change_column_default (must supply a :from and :to option) change_column_null create_join_table create_table disable_extension drop_join_table drop_table (must supply a block) enable_extension remove_column (must supply a type) remove_foreign_key (must supply a second table) remove_index remove_reference remove_timestamps rename_column rename_index rename_table
-
By default, the name of the join table comes from the union of the first two arguments provided to create_join_table
-
-
If the column names can not be derived from the table names, you can use the :column and :primary_key options.
-
-
If your migration is irreversible, you should raise ActiveRecord::IrreversibleMigration from your down method.
-
the db:migrate task also invokes the db:schema:dump task, which will update your db/schema.rb file to match the structure of your database.
-
-
db:reset task will drop the database and set it up again. This is functionally equivalent to rails db:drop db:setup.
-
If you have already run the migration, then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run rails db:migrate.
-
must rollback the migration (for example with bin/rails db:rollback), edit your migration and then run rails db:migrate to run the corrected version.
-
revert method can be helpful when writing a new migration to undo previous migrations in whole or in part
-
Schema files are also useful if you want a quick look at what attributes an Active Record object has
-
annotate_models gem automatically adds and updates comments at the top of each model summarizing the schema if you desire that functionality.
-
db/schema.rb cannot express database specific items such as triggers, stored procedures or check constraints
-
you can execute custom SQL statements, the schema dumper cannot reconstitute those statements from the database
-
-
set in config/application.rb by the config.active_record.schema_format setting, which may be either :sql or :ruby.
-
Validations such as validates :foreign_key, uniqueness: true are one way in which models can enforce data integrity
-
The :dependent option on associations allows models to automatically destroy child objects when the parent is destroyed.
-
To add initial data after a database is created, Rails has a built-in 'seeds' feature that makes the process quick and easy.