问题
I am trying to create a very basic Sinatra app which uses only Active Record and Sqlite3.
To be as complete as possible I am following instructions from a tutorial which states the following steps in order:
- Create a database by putting the following code in the main app file:
ActiveRecord::Base.establish_connection(
:adapter =>'sqlite3',
:database=>'wiki.db'
)
class User < ActiveRecord::Base
validates :username, presence: true, uniqueness: true
validates :password, presence: true
end
- To create the database and alter its structure create a Rakefile with the following contents:
require "./wadapp.rb"
require "sinatra/activerecord/rake"
- Save the Rake file and run the following command in the terminal:
rake db:create_migration NAME=create_users
- A new file will be created in
db/migrate/
calledtimestamp_create_users.rb
Navigate to that file and edit with the following contents:
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :username
t.string :password
t.boolean :edit
t.timestamps null: false
end
User.create(username: "Admin", password: "admin", edit: true)
end
end
- In the terminal run the command
rake db:migrate
.
It is this final point where the code stops working. I get no output at all to indicate a :users
has been created and no table is accessible within the app.
I have tried rolling back etc but there is no indication a table was even created so there is nothing roll back or alter? I also tried running rake db:create
first as some posts suggested but got the following error:
(in /Users/jonathonday/ruby/wad/wiki)
rake aborted!
ActiveRecord::AdapterNotSpecified: The `development` database is not configured for the `default_env` environment.
Available databases configurations are:
/Users/jonathonday/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/connection_specification.rb:251:in `resolve_symbol_connection'
/Users/jonathonday/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/connection_specification.rb:219:in `resolve_connection'
/Users/jonathonday/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/connection_specification.rb:140:in `resolve'
/Users/jonathonday/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.2.1/lib/active_record/connection_handling.rb:187:in `resolve_config_for_connection'
/Users/jonathonday/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.2.1/lib/active_record/connection_handling.rb:50:in `establish_connection'
/Users/jonathonday/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:187:in `create_current'
/Users/jonathonday/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.2.1/lib/active_record/railties/databases.rake:39:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:create
(See full trace by running task with --trace)
I have also run the command rake db:migrate as a root user (sudo su) and did get a different error:
(in /Users/jonathonday/ruby/wad/wiki)
rake aborted!
LoadError: cannot load such file -- sinatra
/Users/jonathonday/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Users/jonathonday/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
This is the order I was given the instructions in to create and build the basic database with ActiveRecord so if anything is missing or there is a better way I will be very happy to listen.
回答1:
I managed to resolve the issue by uninstalling ActiveRecord 6.0.2.1 and installing version 5.2.4.
This seems to resolve all the issues and when I now run rake db:migrate
I get the result:
(in /Users/jonathonday/ruby/wad/wiki)
== 20200209194032 CreateUsers: migrating ======================================
-- create_table(:users)
-> 0.0008s
== 20200209194032 CreateUsers: migrated (0.0178s) =============================
I am not sure why this is but I will post a seperate question on that.
来源:https://stackoverflow.com/questions/60132650/rake-dbmigrate-not-working-when-using-activerecord-with-sinatra