Sqlite 找不到Sinatra activerecord表错误
我有一个简单的sinatra activerecord应用程序。在文件./config/environments.rb中,我有以下配置:Sqlite 找不到Sinatra activerecord表错误,sqlite,activerecord,sinatra,Sqlite,Activerecord,Sinatra,我有一个简单的sinatra activerecord应用程序。在文件./config/environments.rb中,我有以下配置: configure :development, :test do set :database, 'sqlite://database.db' end # These Heroku setup instructions can be at: https://devcenter.heroku.com/articles/rack configure :prod
configure :development, :test do
set :database, 'sqlite://database.db'
end
# These Heroku setup instructions can be at: https://devcenter.heroku.com/articles/rack
configure :production do
# Database connection
db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')
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'
)
end
当我推到Heroku时,DB设置工作正常。然而,在当地,情况并非如此。例如,如果我
$ rake db:migrate
我得到以下良好的输出:
== CreateHotels: migrating ===================================================
-- create_table(:hotels)
-> 0.0015s
-- add_index(:hotels, :hotel_name)
-> 0.0004s
== CreateHotels: migrated (0.0021s) ==========================================
现在,如果我启动服务器并使用curl进行调用:
$ shotgun app.rb
$ curl -d "hotel_name=Sheraton" http://127.0.0.1:9393/hotels/
我得到以下错误:
ActiveRecord::StatementInvalid - Could not find table 'hotels':
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/connection_adapters/sqlite_adapter.rb:472:in `table_structure'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/connection_adapters/sqlite_adapter.rb:346:in `columns'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/model_schema.rb:228:in `yield'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/model_schema.rb:228:in `default'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/model_schema.rb:228:in `columns'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/model_schema.rb:243:in `column_defaults'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.8/lib/active_record/base.rb:482:in `initialize'
/Users/Sy/Developer/Sinatra/stay-checkin-api/routes/hotels_routes.rb:8:in `new'
/Users/Sy/Developer/Sinatra/stay-checkin-api/routes/hotels_routes.rb:8:in `block in <top (required)>'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `block in compile!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `[]'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (3 levels) in route!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:851:in `route_eval'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (2 levels) in route!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:872:in `block in process_route'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `catch'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `process_route'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:834:in `block in route!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `each'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `route!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:936:in `dispatch!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `block in call!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `block in invoke'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `catch'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `invoke'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `call!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:755:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-protection-1.2.0/lib/rack/protection/path_traversal.rb:16:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-protection-1.2.0/lib/rack/protection/json_csrf.rb:17:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/nulllogger.rb:9:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/head.rb:9:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/showexceptions.rb:21:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:99:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `block in call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:1471:in `synchronize'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/lint.rb:48:in `_call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/lint.rb:36:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/showexceptions.rb:24:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/commonlogger.rb:20:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/sinatra-1.3.3/lib/sinatra/base.rb:136:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:86:in `proceed_as_child'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:31:in `call!'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/loader.rb:18:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/lib/shotgun/favicon.rb:12:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/builder.rb:134:in `call'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.4.1/lib/thin/connection.rb:80:in `block in pre_process'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.4.1/lib/thin/connection.rb:78:in `catch'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.4.1/lib/thin/connection.rb:78:in `pre_process'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.4.1/lib/thin/connection.rb:53:in `process'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.4.1/lib/thin/connection.rb:38:in `receive_data'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.4.1/lib/thin/backends/base.rb:63:in `start'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/thin-1.4.1/lib/thin/server.rb:159:in `start'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/rack-1.4.1/lib/rack/handler/thin.rb:13:in `run'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/gems/shotgun-0.9/bin/shotgun:156:in `<top (required)>'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/bin/shotgun:19:in `load'
/Users/Sy/.rvm/gems/ruby-1.9.3-p125/bin/shotgun:19:in `<main>'
config.rb
require 'sinatra'
require 'sinatra/activerecord'
require 'uri'
require './config/environments'
require_relative 'models/init'
require_relative 'routes/init'
require './app'
run Sinatra::Application
class Hotel < ActiveRecord::Base
validates :hotel_name, :presence => true
end
get '/hotels' do
@hotels = Hotel.all
if @hotels
content_type :json
@hotels.to_json
end
end
post '/hotels/' do
@hotel = Hotel.new
@hotel.hotel_name = params[:hotel_name]
if @hotel.save
content_type :json
@hotel.to_json
else
content_type :json
"Failed to create new hotel with name #{params[:hotel_name]}".to_json
end
end
型号:hotel.rb
require 'sinatra'
require 'sinatra/activerecord'
require 'uri'
require './config/environments'
require_relative 'models/init'
require_relative 'routes/init'
require './app'
run Sinatra::Application
class Hotel < ActiveRecord::Base
validates :hotel_name, :presence => true
end
get '/hotels' do
@hotels = Hotel.all
if @hotels
content_type :json
@hotels.to_json
end
end
post '/hotels/' do
@hotel = Hotel.new
@hotel.hotel_name = params[:hotel_name]
if @hotel.save
content_type :json
@hotel.to_json
else
content_type :json
"Failed to create new hotel with name #{params[:hotel_name]}".to_json
end
end
Gemfile
source :rubygems
gem 'heroku'
gem 'sinatra'
gem 'thin'
gem 'activerecord'
gem 'sinatra-activerecord'
gem 'json'
# Define which database gem to use depending on whether you are in local or production environment
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg' # this gem is required to use postgres on Heroku
end
require './app'
require 'sinatra/activerecord/rake'
Rakefile
source :rubygems
gem 'heroku'
gem 'sinatra'
gem 'thin'
gem 'activerecord'
gem 'sinatra-activerecord'
gem 'json'
# Define which database gem to use depending on whether you are in local or production environment
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg' # this gem is required to use postgres on Heroku
end
require './app'
require 'sinatra/activerecord/rake'
数据库集错误,请尝试:
set :database, 'sqlite:///database.db'
有几个问题:1)您是否确认数据库设置在Heroku上运行良好,或者您是否认为这是因为它没有给您任何错误?2) 您是否通过命令行客户端检查过sqlite数据库?我还建议你现在就安装postgres,并根据它进行开发,这并不难,而且从长远来看,如果不是短期的话,也会为你省去麻烦。是的,我对heroku进行了一些api调用,得到了有效的json响应。不太信任,我检查了日志,一切都很好。我想我会听从你的建议,在当地培养博士后。在提出这个问题之前,我做了一系列研究,确实看到几个月前有人说sinatra+sqlite本地不值得这么麻烦。谢谢你的链接!