Ruby, Rake, Mysql - creating database - mysql

How to do?
I tried something like:
RAILS_ENV=production rake db:create db:load
in file /lib/tasks/load_tasks.rake and this file I tried in terminal as rake db:migrate, but I am getting errors about syntax etc.
I entered into terminal this command (I saw it in tutorial):
rails generate scaffold Account user_name:string description:text premium:boolean \
income:integer ranking:float fee:decimal birthday:date login_time:time
And this made me file 20110518181941_create_accounts.rb
How can I create database table - I thought the command above will create me database in mysql... I am now a bit confusing, what to do?
Which rule is playing here rake db:migrate?

I think you are getting this error because your syntax is wrong, please put && between sentences:
RAILS_ENV=production rake db:create && db:schema:load
or do it in separated lines
RAILS_ENV=production rake db:create
RAILS_ENV=production rake db:schema:load
the first command will create the database, the second command will Load the db/schema.rb file into the database
And finally you need to run your migration:
RAILS_ENV=production rake db:migrate
to create your Accounts table.
BTW if you run:
rake -T
you can see the list of rake tasks and their descriptions.
Hope this helps.

Related

rake test:prepare doesn't create test database

I'm on Rails 3.2.9 and when I try to run the following commands
rake db:drop db:create db:schema:load db:migrate test:prepare
rspec spec/
The rspec commands throws an error
Mysql2::Error: Table 'app_test.articles' doesn't exist: SHOW FULL FIELDS FROM `articles` (ActiveRecord::StatementInvalid)
But when I run the test:prepare command seperatly, it works:
rake db:drop db:create db:schema:load db:migrate
rake test:prepare
rspec spec/
Why does preparing the test database in the same rake command not work?
You need to provide the environment information. Like RAILS_ENV=test.
Please try this:
rake db:drop db:create db:test:prepare

RoR Rake 10.4.2 Error: "Don't know how to build task 'db:'"

I want to create a MySQL database in Ruby on Rails using:
rake db: create
But it gives me out an error:
rake aborted! Don't know how to build task 'db:'
I am running Ubuntu and cant find answers on the internet about my version of rake (10.4.2).
It's rake db:create without a blank between db: and create.
If you type rake -T you can find the available rake tasks with their proper names.

Mysql2::Error: Table doesn't exist: SHOW FULL FIELDS FROM `sessions`

I just cloned an application from my developer and I want to set it up on my computer. I get this error when I go to localhost:3000, and I don't really understand what it means.
Error:
ActiveRecord::StatementInvalid - Mysql2::Error: Table
'goacquire_development.sessions' doesn't exist: SHOW FULL FIELDS FROM `sessions`:
I think it could be my installation process, I'll appreciate if someone could explain this error to me.
A few ideas:
Make sure MySQL is running on your local machine.
Make sure you have run rake db:create and rake db:migrate.
Make sure you have a database.yml file configured with MySQL settings.
Hope this helps.
Install MySQL server on your machine
Config database.yml (Create new if your app don't have.)
Run commands:
# cd /path/to/your/app
# bundle install
# rake db:create
# rake db:migrate
# rails s

Rails app error - ActiveRecord::PendingMigrationError Migrations are pending; run 'rake db:migrate RAILS_ENV=development' to resolve this issue

The database is created, tables are created, data are there.
But after I restarted the Rails application, I got this error. The app is using MySQL.
What's the issue here?
Thank you in advance
Solution
Simply run
rake db:migrate
There are migrations that need to be ran before your server can start. Learn what migrations are and how they are used in Rails with this RailsGuide.
Run that rake db:migrate command any time you make a migration, or any time you create a new project. If you get an error saying that migrations are pending, this is the answer. Then try to start the server again.
Only if that doesn't work, try what is below.
If that doesn't work
Run these commands in this order:
rake db:drop
rake db:create
rake db:migrate
rake db:seed - This one is only necessary if you have seeded data.
Remember, when in doubt: restart the server!
just run rake db:migrate then start server, see then what happens, looks like existing migrations are yet not part of schema. Or you schema_migrations table might be missing some version value.
Try to run bundle exec rake db:migrate RAILS_ENV=developmentfor me running just bundle exec rake db:migrate didn't resolve the issue
As one of the comments above, utilizing the following works for me:
rake db:drop
rake db:create
rake db:migrate
when rake db:migrate or restarting didn't help.

RAIL_ENV=production rake db:schema:load aborted with rails 3.1.0 and ubuntu 10.04

Trying to update the production mysql database by doing: rvmsudo RAILS_ENV=production rake db:schema:load on ubuntu 10.04 with rails 3.1.0. The purpose of update is to add a table. The mysql db already exists. Here is the error with trace:
dtt#ubuntu:/var/www/mylab/current$ rvmsudo RAILS_ENV=production rake db:schema:load --trace
** Invoke db:schema:load (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:schema:load
-- create_table("categories", {:force=>true})
rake aborted!
Mysql2::Error: DROP command denied to user 'dtt'#'localhost' for table 'categories': DROP TABLE `categories`
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query'
However running rake command without RAILS_ENV=production is fine without error. But it only created the development db.
Any thoughts about the error? Thanks.
The user you have configured for your production environment in config/database.yml does not have permission to perform the necessary actions on the production environment. Either you need to change your MySQL setup or use a different user.