gpt4 book ai didi

ruby - 使用 ActiveRecord 时 createdb 未被识别为命令

转载 作者:行者123 更新时间:2023-11-29 13:56:30 26 4
gpt4 key购买 nike

我是 Ruby 和网络开发的新手。我正在使用带有 Ruby 2.0 的 Windows 7 64 位,并且安装了 PostgreSQL 9.4。

我正在尝试使用 ActiveRecord 创建数据库。我检查了我的 postgresql 服务器是否正在运行,并且执行了 bundle install 以确保我拥有所有必需的 gem。但是,当我尝试执行终端命令“bundle exec rake create:db”时,它告诉我“'createdb' 未被识别为内部或外部命令、可运行程序或批处理文件。”我还使用 --trace 执行了命令,但它没有提供有关问题所在的更多有用输出。终端只显示这个:

C:\Users\MH\Desktop\activerecord-template> bundle exec rake db:create
Creating activerecord-template development and test databases if they don't exist...
'createdb' is not recognized as an internal or external command, operable program or batch file.

C:\Users\MH\Desktop\activerecord-template> bundle exec rake db:create --trace
**Invoke db:create (first_time)
**Execute db:create
'createdb' is not recognized as an internal or external command, operable program or batch file.

关于此问题,我发现的最接近的内容位于此链接:http://bobbyong.com/blog/installing-postgresql-on-windoes/ .我确实按照链接中的描述调整了 PostGreSQL 的路径,但我仍然遇到相同的 createdb 问题。我还卸载/重新安装了 PostGreSQL。我可以在 PostGreSQL 目录中看到一个 createdb 文件,当我使用 psql 时,createdb 用作命令,所以我不确定 ActiveRecord 到底是什么问题。

这是我的 Gemfile 中的内容:

source 'https://rubygems.org'

gem 'activerecord'
gem 'pg'

gem 'rspec'
gem 'faker'

gem 'rake'

这是我的 Rakefile 中的内容:

    require 'rake'
require 'rspec/core/rake_task'
require 'active_support'
require 'active_support/core_ext'

require_relative 'config'

namespace :db do
desc "Drop, create, and migrate the database"
task :reset => [:drop, :create, :migrate]

desc "Create #{APP_NAME} databases"
task "create" do
puts "Creating #{APP_NAME} development and test databases if they don't exist..."
system("createdb #{DB_NAME} --username #{DB_USERNAME} -w --no-password && createdb #{TEST_DB_NAME} --username #{DB_USERNAME} -w --no-password")
end

desc "Drop #{APP_NAME} databases"
task "drop" do
puts "Dropping #{APP_NAME} development and test databases..."
system("dropdb #{DB_NAME} && dropdb #{TEST_DB_NAME}_test")
end

desc "Migrate the database"
task "migrate" do
ActiveRecord::Migrator.migrations_paths << File.dirname(__FILE__) + 'db/migrate'
ActiveRecord::Migration.verbose = true
ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, nil)
end

desc "Populate the database with sample data"
task "seed" do
require APP_ROOT.join('db', 'seeds.rb')
end
end

namespace :generate do
desc "Create a database migration\n rake generate:migration NAME=create_people"
task :migration do
unless ENV.has_key?('NAME')
raise "Must specify NAME for migration, e.g. rake generate:migration NAME=create_people"
end

migration_name = ENV['NAME']
class_name = migration_name.camelize
timestamp = Time.now.strftime('%Y%m%d%H%M%S')
filename = "#{timestamp}_#{migration_name}.rb"
path = APP_ROOT.join('db', 'migrate', filename)

if File.exist?(path)
raise "ERROR! File '#{path}' already exists"
end

puts "Creating migration at #{path}"
File.open(path, 'w+') do |f|
f.write("class #{class_name} < ActiveRecord::Migration\n\tdef change\n\n\tend\nend")
end
end
end

desc 'Start IRB with application environment loaded'
task "console" do
exec "irb -r./config"
end

desc "Run the specs"
RSpec::Core::RakeTask.new(:spec)
task :default => :specs

# Will this not work?
#desc "Run the specs"
#task 'specs' do
# exec "rspec spec"
#end

这是我的 config.rb 文件中的内容:

require 'pathname'
require 'pg'
require 'active_record'
require 'logger'

## Load all files and configure the db

APP_ROOT = Pathname.new(File.expand_path(File.dirname(__FILE__)))

APP_NAME = APP_ROOT.basename.to_s

DB_PATH = APP_ROOT.join('db', APP_NAME + "_development.db").to_s

DB_NAME = APP_NAME + "_development.db"

TEST_DB_NAME = APP_NAME + "_test.db"

DB_USERNAME = 'postgres'

DB_PASSWORD =

if ENV['DEBUG']
ActiveRecord::Base.logger = Logger.new(STDOUT)
end


Dir[APP_ROOT.join('models', '*.rb')].each do |model_file|
filename = File.basename(model_file).gsub('.rb', '')
autoload ActiveSupport::Inflector.camelize(filename), model_file
end

ActiveRecord::Base.establish_connection :adapter => 'postgresql',
:database => DB_NAME,
:host => 'localhost',
:username => DB_USERNAME,
:password => DB_PASSWORD

最佳答案

如果终端命令“bundle exec rake create:db”产生错误“'createdb'不是内部或外部命令、可运行程序或批处理文件。”,这意味着 ActiveRecord 无法找到 createdb .exe 在 PostgreSQL 目录中。

您必须按照此处所述将 PostgreSQL bin 和 lib 文件夹附加到您的路径环境变量:http://bobbyong.com/blog/installing-postgresql-on-windoes/

请注意,将 bin 路径放在 lib 路径之前很重要,否则 ActiveRecord 仍然无法在 bin 路径中找到 createdb.exe。还要确保重新启动命令终端,以便对环境变量所做的任何更改都能生效。

关于ruby - 使用 ActiveRecord 时 createdb 未被识别为命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30742806/

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