简体   繁体   English

如何正确使用database.yml文件从MySQL连接到Ruby on rails?

[英]How to connect from MySQL to Ruby on rails using database.yml file properly?

I have a problem with the connection between MySQL and Ruby on rails.我对 MySQL 和 Ruby on rails 之间的连接有问题。

I have a database in MySQL, and I need to migrate it to ActiveRecord in Ruby on rails.我在 MySQL 中有一个数据库,我需要将它迁移到 Ruby on rails 中的 ActiveRecord。 To do that, I have to change the database.yml file in my ruby on rails project, like this:为此,我必须更改 ruby​​ on rails 项目中的 database.yml 文件,如下所示:

# 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.问题出在 Ruby on rails 控制台中。 The migration isn't run properly, and all the IDs are null, regardless of the table I need to migrate.迁移运行不正常,所有的 ID 都是空的,不管我需要迁移的表是什么。 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.在任何表中,所有 ID 都为空,因此我无法列出我在数据库中引入的最后一个用户,因为我需要使用 ID 执行 Order by。 In the Gemfile, I have the gem mysql2 .在 Gemfile 中,我有 gem mysql2

What am I doing wrong?我究竟做错了什么?

Firstly you need to install the gem.首先你需要安装gem。 Install the latest gem in my case I have given the latest version在我的情况下安装最新的 gem 我已经给出了最新版本

gem 'mysql2','~> 0.3.20'


Then you need to configure your database.yml file like this然后你需要像这样配置你的database.yml文件

# 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.然后使用rake db:createrails db:create 创建数据库,然后您可以使用数据库进行开发和测试。

At last, i can migrate properly from mysql to ActiveRecords, thanks you all.最后,我可以正确地从 mysql 迁移到 ActiveRecords,谢谢大家。 The connection between both with the database.yml was properly constructed, the problem came from the database in mysql.两者与database.yml之间的连接构建正确,问题来自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:在mysql中,我有一个完整的数据库,但我只需要迁移带有子查询的表,问题是新表没有正确继承主键或外键,因为表是直接从子查询创建的,我教你如何:

This is an image, with a query whit colums from table user: show columns from users, from the first database:这是一个图像,带有来自表 user 的查询 whit 列:显示来自用户的列,来自第一个数据库:

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.为了解决它,我没有使用子查询,而是创建一个尊重外键和构造的新表,然后使用 INSERT INTO 子句添加新行。

Thanks you all.谢谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正确 Ruby on Rails Database.yml 文件的 MySQL 配置 - Correct MySQL configuration for Ruby on Rails Database.yml file 如何管理Rails database.yml - How to manage Rails database.yml Ruby on Rails database.yml中的socket声明是什么? - What is the socket declaration for, in Ruby on Rails database.yml? Docker + Rails + MySQL = 忽略 database.yml 中的环境变量 - Docker + Rails + MySQL = Ignoring environment variables in database.yml Rails - 具有单个源和database.yml文件的多个开发实例 - Rails - Multiple development instances with a single source and database.yml file Rails正在寻找未由database.yml指定的数据库 - Rails looking for a databases not specified by database.yml Ruby on Rails“解析config / database.yml时发生YAML语法错误” - “YAML syntax error occurred while parsing config/database.yml” Ruby on Rails 缺少项目中的database.yml文件 - Missing database.yml file in a project 与Homebrew一起安装MySQL。 要连接的默认database.yml配置是什么? - Installed MySQL with Homebrew. What is the default database.yml configuration to connect? 为什么某些Rails项目使用具有不同扩展名(mysql,postgresql等)的多个database.yml文件? - Why do some Rails projects use multiple database.yml files with different extensions (mysql, postgresql etc)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM