2010-07-25 52 views
2

我生成一個腳手架,並把類型:值,但現在我需要添加另一個字段/數據庫列如何添加另一個類型:價值沒有銷燬和重新啓動我的整個項目?RoR腳手架添加字段後腳本/生成

rake aborted! 
Mysql::Error: You have an error in your SQL syntax; check the manual that corres 
ponds to your MySQL server version for the right syntax to use near '(11), `titl 
e` varchar(255) DEFAULT NULL, `artist_old` varchar(255) DEFAULT NULL,' at line 1 
: CREATE TABLE `albums` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY(11 
), `title` varchar(255) DEFAULT NULL, `artist_old` varchar(255) DEFAULT NULL, `r 
elease_date` datetime DEFAULT NULL, `genre` varchar(255) DEFAULT NULL, `feature` 
int(11) DEFAULT NULL, `image_path` varchar(255) DEFAULT NULL, `created_at` date 
time DEFAULT NULL, `updated_at` datetime DEFAULT NULL, `artist_id` int(11) DEFAU 
LT NULL) ENGINE=InnoDB 

回答

5

通常,當您使用支架的命令,它會在您的db/migrate/文件夾遷移,包含了所有的數據庫設置你的模型,例如:

class CreateComments < ActiveRecord::Migration 
    def self.up 
    create_table :comments do |t| 
     t.text :body 
    end 
    end 

    def self.down 
    drop_table :comments 
    end 
end 

如果您還沒有成功後運行rake db:migrate命令您創建了腳手架,您可以在db/migrate/下方輕鬆編輯遷移文件,並在開始時添加您錯過的字段。編輯完文件後,運行rake db:migrate命令將遷移應用到數據庫。

如果您在創建腳手架後已經通過了rake db:migrate,則可以使用script/generate migration AddSubjectColumnToComments創建一個新的遷移,以向表中添加另一個字段。在我上面的例子中,我會得到一個新的遷移,並填寫以下代碼:

class AddSubjectColumnToComments < ActiveRecord::Migration 
    def self.up 
    add_column :subject, :comments, :string 
    end 

    def self.down 
    remove_column :subject, :comments 
    end 

祝你好運遷移!

1

只需生成遷移並手動更新您的視圖。

+0

更新*耙正在返回一個MySQL錯誤通知有什麼奇怪的? – ThomasReggi 2010-07-25 16:45:34

+0

什麼是rake命令? – Eimantas 2010-07-25 16:48:14