簡體   English   中英

使用ActiveRecord時createdb無法識別為命令

[英]createdb not recognized as a command when using ActiveRecord

我是Ruby和Web開發的新手。 我將Windows 7 64位和Ruby 2.0一起使用,並且安裝了PostgreSQL 9.4。

我正在嘗試使用ActiveRecord創建數據庫。 我檢查我的postgresql服務器是否正在運行,並進行捆綁安裝以確保擁有所有必需的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。 另外,請確保重新啟動命令終端,以便對環境變量所做的任何更改都可以生效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM