Rails migration
2015-11-18 00:00
477 查看
=Active Model Migration(数据库迁移)= (!!!: type 是保留字段,不能作为模型的列。) Active Record 会自动追踪哪些 Migrations 已经执行过、哪些还没执行。所以,你只要更新 你本地的代码然后执行 rake db:migrate ,其他的就交给 Active Record ,它会自己搞懂该 跑哪些 Migrations 。还有,它也会自动更新 db/schema.rb 文件,让它与修改后的数据库结构同步。 $ rails generate migration AddPartNumberToProducts part_number:string ;增加内容 $ rails generate migration RemovePartNumberFromProducts part_number:string ;删除内容 $ rails generate migration AddDetailsToProducts part_number:string price:decimal ;操作多个字段 $ bin/rake db:migrate ; 完成资料库,可以访问 http://localhost:3000/people 进行操作了。 ; 新生成了数据库迁移文件,需要运行迁移文件来完成迁移。 $ rake db:migrate VERSION=20080906120000 ;执行Migrations $ rake db:rollback ;回滚 $ rake db:rollback STEP=3 ;多个migration $ rake db:migrate:status #迁移状态 $ rake db:migrate VERSION=0 #不执行任何migrate $ rake db:migrate:redo VERSION=my_version #重做,执行 特定的文件,指定它的版本 $ db:migrate:up VERSION=20090408054532 #up方法定义要对数据库模式做哪些操作 $ db:migrate:down VERSION=my_version #down方法来撤销哪些操作 更改表字段名字 1.rails g migration RenameColumn RenameColumn 这个名称随意,但要能被读懂。这个命令会产生一个migrate文件. 2.增加migrate文件,里面写上要进行的操作 class RenameColumn < ActiveRecord::Migration def change rename_column :addresses, :type, :addr_type end end 其中 change 行格式: rename_column :table_name, :old_column, :new_column Notice:此处表名为复数。 去掉某列 rails generate migration RemoveFieldNameFromTableName field_name:datatype 或者在migratation的文件种声明 remove_column :table_name, :column_name, :column_type 增加某列 rails g migration AddDefaultToAddress default:boolean:false add_column :addresses, :default,:boolean,:default => false ==Column migration methods== add_column(table , column , type , options) remove_column(table , column, ) rename_column(table , column , new_name) change_column ( table , column , type , options ) ==Index migration metods== add_index( table , column , options ) remove_index ( table , column) #options list : :unique => true/false :name => "your_custom_name" class AddPhoneIndexToUser < ActiveRecord::Migration def change add_column :users, :phone,:string,:default => false add_index :users, :phone , unique: true end end
相关文章推荐
- Rails Kinds
- 两种智能指针-RAII智能指针和引用计数智能指针
- AVL树笔记(二):maintain,delete
- (转)RabbitMQ消息队列(一): Detailed Introduction 详细介绍
- Once you eliminate all the other factors,the only thing remaining must be the truth.
- fail-fast机制
- Kafka设计解析(二)- Kafka High Availability (上)
- 关于int main(int argc,char*argv[])的玩意
- hdu 2389 Rain on your Parade 二分图匹配+HK算法
- Adb failed to restart! Make sure the plugin is properly configured.
- 关于container_of和list_for_each_entry 及其相关函数的分析
- OC中retain和assign的setter方法的写法和区别
- 【MFC】关于AfxGetMainWnd导致VS2010运行结果崩溃的解法
- Daily Scrum - 11/17
- http://blog.csdn.net/lifeitengup/article/details/8666182
- HDU---1151-Air Raid(二分图)(HDU-1150)(POJ-3041)
- POJ 1422 Air Raid 最小覆盖点
- pthread_cond_wait
- UVA 题目10617 - Again Palindrome(区间DP)
- poj 1422 Air Raid(最小路径覆盖)