rake db:create throws “database does not exist” error with postgresql

前端 未结 10 899
忘了有多久
忘了有多久 2021-01-30 19:58

I\'m using rails 4.1.5 with postgresql 9.1 under Debian 7, and I\'m not able to create a database in my development environment. When I run

bin/rake db:create
         


        
10条回答
  •  醉梦人生
    2021-01-30 20:53

    I had the same problem and in my case, I had used Answer.column_name in a validation in the Answer model itself. Because test database was already created on my local so RAILS_ENV=test rails db:create was working fine on local, but giving the same error in CI/CD pipeline

    It was due to the reason that Rails load all the files inside app/ directory before running db:create command, and at that time as no database is there Answer.column_names failed.

    I used something like this:

      validates_uniqueness_of :content, scope: (Answer.column_names - %w[id created_at updated_at]).map(&:to_sym), message: 'Duplicate answer'
    

    which is wrong. Then I changed to:

    DUPLICATE_ANSWER_SCOPE = %i[content question_id session_id]
    validates_uniqueness_of :content, scope: DUPLICATE_ANSWER_SCOPE, message: 'Duplicate answer'
    

提交回复
热议问题