-1

I have a problem with the connection between MySQL and Ruby on rails.

I have a database in MySQL, and I need to migrate it to ActiveRecord in Ruby on rails. To do that, I have to change the database.yml file in my ruby on rails project, like this:

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  adapter: mysql2
  database: ********
  username: ******
  password: ******
  host: *******

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

Obviously, the * characters are information I don´t need to show, but it's properly constructed, and the connection is established as usual.

The problem comes in Ruby on rails console. The migration isn't run properly, and all the IDs are null, regardless of the table I need to migrate. For example, I take this from a table:

ID_NILL

All the ID are null, in any table, so I can't, for example, list the last User I introduced in the database, because I would need to do a Order by with the ID. In the Gemfile, I have the gem mysql2.

What am I doing wrong?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Alvaro
  • 11
  • 3

2 Answers2

1

Firstly you need to install the gem. Install the latest gem in my case I have given the latest version

gem 'mysql2','~> 0.3.20'


Then you need to configure your database.yml file like this

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: mysql2
  pool: 5
  timeout: 5000
  username: username
  password: password

development:
  <<: *default
  database: database/development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: test/development

production:
  <<: *default
  database: db/production


Then create the database using rake db:create or rails db:create and then you can use database both development and test.

Aniket Tiwari
  • 3,561
  • 4
  • 21
  • 61
0

At last, i can migrate properly from mysql to ActiveRecords, thanks you all. The connection between both with the database.yml was properly constructed, the problem came from the database in mysql.

In mysql, i have a complete database, but i need to migrate only tables with subquery, the problem is that the new tables don´t inherit properly the primary key or the foreign key, because the tables are created from thats subquerys directly, i show you how:

This is an image, with a query whit colums from table user: show columns from users, from the first database:

Correct Table from Users

An this, is an image taken from the new user tables, generated directly from a subquery against the first user table:

Incorrect Table from Users

As you can see, only inherit the types and limits of all the atributes, but don´t inherit nothing more

For resolve it, instead of use a subquery, i create a new table respecting the foreign key and constract, and then, add the new rows using a INSERT INTO clausule.

Thanks you all.

Alvaro
  • 11
  • 3