2016-12-30 74 views
1

我部署到Heroku的,我需要翻譯一些Ruby的示例代碼的SQLite到Postgres的。紅寶石:翻譯sqlite的命令,命令的Postgres

這是原來的SQLite代碼:

#SQLite table 
DB = Sequel.connect('sqlite://gcm-test.db') 
DB.create_table? :Device do 
    primary_key :reg_id 
    String :user_id 
    String :reg_token 
    String :os, :default => 'android' 
end 
Device = DB[:Device] # create the dataset 

我翻譯了創建表的一部分,但我不知道是什麼的最後一行表示。

Device = DB[:Device] # create the dataset 

這是翻譯的Postgres的代碼是什麼樣子至今:

DB = PG.connect(ENV['DATABASE_URL']) 
DB.exec "DROP TABLE IF EXISTS Device" 
DB.exec "CREATE TABLE Device(reg_id INTEGER PRIMARY KEY, user_id text, reg_token text, os text DEFAULT 'android')" 
Device = DB[:Device] # create the dataset 

當我嘗試在Heroku上本地運行它,我得到這個錯誤:

in `<main>': undefined method `[]' for #<PG::Connection:0x007fe7ca7a6610> (NoMethodError) 

有誰知道如何處理這條線?

+2

Ruby續集應該爲你做這個。是否有一個原因,你不只是使用Postgres與SQLite的Ruby Sequel?您可以將「Sequel.connect」用於各種SQL後端,而不僅僅是SQLite。例如'DB = Sequel.connect(「postgres:// localhost/mydb」)' – Joe

+0

好的謝謝。我認爲PG數據庫正在創建好。這只是這條線給出的錯誤:Device = DB [:Device]#創建數據集 – mjpablo23

+1

重點在於,Sequel庫除了一些基本的SQL以外還有許多額外的工作。數據集處理非常廣泛和動態。你基本上用本機Postgres客戶端庫替換了Sequel ORM庫,並且它完成了它(並且它完成了很多操作)。你必須用你自己的代碼重新實現Ruby Sequel中的所有數據集處理。 – Joe

回答

0

剛剛將其更改爲

DB = Sequel.connect(ENV['DATABASE_URL']), 

喬說。