Heroku and Datamapper problems

2019-05-10 11:02发布

问题:

I can launch a basic app on Heroku, displaying a message with get '/'... works just fine. However, whenever I try to add sqlite with datamapper, the thing breaks down.

In order to see my app structure, check out the project on github. I've kept the code pretty bare-bones.

In the log from heroku I'm getting:

2011-06-26T21:28:36+00:00 app[web.1]: /app/.bundle/gems/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/adapters.rb:163:in `require': no such file to load -- dm-postgres-adapter (LoadError)

The thing about this is that I'm not using postgres, so I'm confused why it is saying this.

回答1:

"The thing about this is that I'm not using postgres, so I'm confused why it is saying this."

You're using Heroku, so you are using Postgresql.

In your app.rb you have this line:

DataMapper::setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/notes.db")

which I'm guessing you got from the Heroku database docs. This line basically says "check for the environment variable 'DATABASE_URL', and if it's set use it as the database url, otherwise use the Sqlite url". Running locally this environment variable won't be set, so you'll use the Sqlite url, but on Heroku this will be set to something like (see the page linked above):

postgres://username:password@hostname/database

Datamapper will see that this is a Postgresql url, and try to require the postgres adapter, which isn't installed so will result in the error that you see.

The best solution would be to install Postgresql locally, so that your development and production environments are as similar as possible. If you can't do this, or don't want to, you can specify the Sqlite adapter locally, and the Postgres adapter in production. It'll look something like this in your Gemfile:

group :development do
  gem 'dm-sqlite-adapter'
end

group :production do
  gem 'dm-postgres-adapter'
end

If you do this you'll need to tell Heroku to which groups to leave out when installing gems, see the Heroku Gem Bunder docs.



回答2:

You are moving from sqlite to postgres-sql and thus you should include dm-postgres-adapter in your Gemfile (and install it ofcourse).



回答3:

Heroku only supports a read-only file system (or at least only supports read-only operations for any files that you want to be persistent). Hence, you can't use SQLite on Heroku. Heroku only supports PostgreSQL as a database so you need to configure DataMapper to use PostgreSQL (see Yet Another Geek's answer).

I'd recommend installing PostgreSQL in your development environment if you're planning to deploy to Heroku. Every database is a little different and no ORM will protect you from the differences. A bit of searching for questions tagged heroku and postgresql should show some of the problems you'll have if you develop on top of one database but deploy on another.