rail 生成表 加欄位

2021-09-11 07:42:41 字數 1179 閱讀 1261

建立表方法:

rails generate model experts

在db/migrate/2019***_create_experts.rb 檔案中,

class createexperts < activerecord::migration

def change

create_table 'experts' do |t|

t.string 'name'

t.string 'image',comment: '頭像'

t.string 'university',comment: '畢業學校'

t.text 'introduction',comment: '簡介'

t.string 'company',comment: '公司'

t.string 'title',comment: '頭銜'

t.integer 'position',comment: '權重'

t.integer 'is_blocked',limit: 4,default:0,comment: '是否遮蔽;0:不屏弊;1:遮蔽'

t.integer 'class', default:1, comment: '專家分類.1:首席經濟學家/經濟;2.**分析師/投資顧問委員會;3.自**大v'

t.timestamps null: false

endend

end

執行rake db:migrate

增加字段,兩種方法:

4.rails g migration [名字] [欄位名]:[型別], [欄位名]:[型別]

5. rails generate migration add_class_to_experts 然後在檔案中寫

class addclasstoexperts < activerecord::migration

def change

add_column :experts, :class, :integer, default:1, comment: '專家分類.1:首席經濟學家/經濟;2.**分析師/投資顧問委員會;3.自**大v'

endend

rails generate migration add_password_digest_to_users

mysql給表的字段加索引

1 新增普通索引 alter table table name add index index name column 2 新增主鍵索引 alter table table name addprimary key column 3 新增唯一索引 unique alter table table na...

MyISAM表加字段的特殊方法

最近乙個統計系統的大表需要加欄位,表的引擎是myisam,表大小在3億,物理檔案在106g。想想都蛋疼。那麼這種情況下怎麼把字段擼上去呢?1.首先想到了 高效能mysql 提到的直接更改表結構檔案 frm 但是在經過測試以後,發現提示表損壞了,需要repair,只好放棄了。2.使用pt online...

已有大量資料的表加字段的方法

如果乙個表中有大量的資料庫,增加欄位或增加字段長度,經常會彈出以下錯誤 超時時間已到,無法修改表 用以下方法可解決問題 增加字段 alter table 表名 add 字段 字段型別 例如 use ase order alter table dbo.t orderbase add remaronly...