2015-07-04 51 views
0

我想創建使用Sinatra和Sinatra /活動記錄的活動記錄,我的遷移正在遷移到Postgres數據庫,但表沒有在數據庫中創建,我經歷了所有可能的堆棧溢出解決方案,但沒用。我嘗試甚至從db/migrate文件夾中刪除我的遷移文件,但仍然是相同的輸出。什麼一定是錯誤遷移後創建Sinatra db表

的Gemfile

source 'https://rubygems.org' 

gem "sinatra" 
gem "pg" #for postgres 
gem "activerecord" 
gem "sinatra-activerecord" 

config.ru

require "./app" 
run Sinatra::Application 

rakefile.rb

require "./app" 
require "sinatra/activerecord/rake" 

app.rb

require 'sinatra' 
require 'sinatra/activerecord' 
db = URI.parse('postgres://project1:[email protected]/*****') 
ActiveRecord::Base.establish_connection(
    :adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme, 
    :host  => db.host, 
    :username => db.user, 
    :password => db.password, 
    :database => db.path[1..-1], 
    :encoding => 'utf8' 
) 


class Note < ActiveRecord::Base 
end 

class CreateNotes < ActiveRecord::Migration 
    def up 
    create_table :notes do |t| 
     t.string :title 
     t.text :body 
     t.timestamps 
    end 
    end 
    def down 
    drop_table :notes 
    end 
end 

遷移的輸出

[email protected]:~/rails-apps/project1$ rake db:migrate 
== 20150704053019 CreateNotes: migrating  ====================================== 
== 20150704053019 CreateNotes: migrated (0.0000s) ============================= 

分貝輸出(PSQL)

\dt 

      List of relations 
Schema |  Name  | Type | Owner 
--------+-------------------+-------+----------- 
public | schema_migrations | table | project1 
(1 row) 

project1=# select * from schema_migrations; 
    version  
---------------- 
20150704053019 

(1行)

注: PROJECT1用戶與所有權限的超級用戶

編輯

遷移文件20150704053019_create_notes.rb

class CreateNotes < ActiveRecord::Migration 
    def change 
    end 
end 
+0

我想你已經嘗試過'rake db:rollback'已經,然後重新運行? –

+0

你可以發佈遷移文件嗎? – thesecretmaster

+1

您用於創建筆記表的遷移實際上保存在'app.rb'中。但遷移任務會在'db/migrate'內查找遷移。如果您在底部給出的遷移文件位於正確的目錄中,請將遷移定義從app.rb移至此處。 – limekin

回答

2

我想首先要注意的是@limekin是第一得到答案,評論:

您用於創建便籤表的遷移實際上保存在app.rb中。但遷移任務會在db/migrate中查找遷移。如果您在底部給出的遷移文件位於正確的目錄中,請將遷移定義從app.rb移至此處。

我只是想了解更多的細節。您用於創建表的功能屬於遷移文件,因爲遷移是創建,更改和刪除表,列和記錄的功能。

因此,要解決您的問題,只需將您的應用程序文件中的函數移動到遷移文件中的更改函數中即可。