gpt4 book ai didi

ruby-on-rails-3 - Rails 迁移 : establish_connection creates table in wrong database

转载 作者:行者123 更新时间:2023-12-01 12:51:41 25 4
gpt4 key购买 nike

我想在我的 database.yml 文件中定义的数据库以外的数据库中创建一个新表。

这是我的 database.yml 文件:

development:                                                                                                                                                                    
adapter: mysql2
encoding: utf8
reconnect: false
database: main_development
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock

test:
adapter: mysql2
encoding: utf8
reconnect: false
database: main_test
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock

production:
adapter: mysql2
encoding: utf8
reconnect: false
database: main_prod
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock

我有另一个名为“外围”的数据库。我想在该数据库中创建一个名为“retailer_to_domain”的表。

这是我的迁移文件:

class CreateRetailerToDomains < ActiveRecord::Migration                                                                                                                         

def connection
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:encoding => "utf8",
:reconnect => false,
:database => "peripheral",
:pool => 5,
:username => "root",
:password => "",
:socket => "/var/run/mysqld/mysqld.sock").connection
end

def change
ActiveRecord::Base.connection.create_table :retailer_to_domains do |t|
t.string :name
t.string :domain

t.timestamps
end
end
end

迁移文件由命令生成:rails generate model RetailerToDomain name:string domain:string 然后我添加了 def connection 方法来覆盖默认数据库( “main”)来自 database.yml 配置。

当我运行迁移 (rake db:migrate) 时,在 main_development 数据库中创建了 retailer_to_domains 表。我如何覆盖此默认值以使迁移在我想要的位置创建表?

此外,我希望 RetailerToDomain 模型以类似的方式使用 establish_connection 方法访问此表,如下所示:

class RetailerToDomain < ActiveRecord::Base                                                                                                                                     

establish_connection(
:adapter => "mysql2",
:encoding => "utf8",
:reconnect => false,
:database => "peripheral",
:pool => 5,
:username => "root",
:password => "",
:socket => "/var/run/mysqld/mysqld.sock")

self.table=retailer_to_domain

validates_presence_of :name, :domain

end

在此先感谢您的任何想法!

最佳答案

观看此页面!

https://github.com/rails/rails/issues/3497

创建rake文件

# Augment the main migration to migrate your engine, too.
task 'db:migrate', 'your_engine:db:migrate'

# Augment to dump/load the engine schema, too
task 'db:schema:dump', 'your_engine:db:schema:dump'
task 'db:schema:load', 'your_engine:db:schema:load'

namespace :your_engine do
namespace :db do
desc 'Migrates the your_engine database'
task :migrate => :environment do
p "your_engine db migrate"
with_engine_connection do
ActiveRecord::Migrator.migrate("#{File.dirname(__FILE__)}/../../db/migrate/your_engine", ENV['VERSION'].try(:to_i))
end
Rake::Task['your_engine:db:schema:dump'].invoke
end

task :'schema:dump' => :environment do
require 'active_record/schema_dumper'

with_engine_connection do
File.open(File.expand_path('../../../db/your_engine_schema.rb', __FILE__), 'w') do |file|
ActiveRecord::SchemaDumper.dump ActiveRecord::Base.connection, file
end
end
end

task :'schema:load' => :environment do
with_engine_connection do
load File.expand_path('../../../db/your_engine_schema.rb', __FILE__)
end
end
end
end

# Hack to temporarily connect AR::Base to your_engine.
def with_engine_connection
original = ActiveRecord::Base.remove_connection
ActiveRecord::Base.establish_connection "your_engine_#{Rails.env}".to_sym
yield
ensure
ActiveRecord::Base.establish_connection original
end

模型

class YourModel < ActiveRecord::Base
establish_connection "your_engine_#{Rails.env}".tom_sym
end

数据库.yaml

your_engine_development:
adapter:
database:
encoding:
username:
password:
host:

关于ruby-on-rails-3 - Rails 迁移 : establish_connection creates table in wrong database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12021715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com