我正在開發一個Rails API並和Heroku一起玩。所有環境都使用Postgres。我在rake db中得到同樣的錯誤:setup和rake db:test:prepare(和'heroku run rake db:setup')。當我第一次嘗試推向Heroku時,這個問題最初出現了。我在本地使用mysql。由於已經遷移開發和測試,以Postgres的避免像這些在未來的驚喜...爲什麼我在使用Postgresql的Rails應用程序中使用布爾型字段時收到ActiveRecord :: StatementInvalid:PG :: SyntaxError:ERROR?
$ rake db:test:prepare
rake aborted!
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "("
LINE 1: ...r, "borrower_id" integer, "lenderAccepted" boolean(1), "crea...
^
: CREATE TABLE "bookings" ("id" serial primary key, "pickup" timestamp, "hoursBooked" integer, "lender_id" integer, "borrower_id" integer, "lenderAccepted" boolean(1), "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, "equipment_id" integer)
這裏是遷移:
class CreateBookings < ActiveRecord::Migration
def change
create_table :bookings do |t|
t.datetime :pickup
t.integer :hoursBooked
t.references :lender, index: true
t.references :borrower, index: true
t.boolean :lenderAccepted
t.timestamps null: false
end
add_foreign_key :bookings, :lenders
add_foreign_key :bookings, :borrowers
end
end
這裏是我的Gemfile:
source 'https://rubygems.org'
gem 'rails', '4.2.0'
gem 'sass-rails', '5.0.0.beta1'
gem 'uglifier', '2.5.3'
gem 'coffee-rails', '4.1.0'
gem 'jquery-rails', '4.0.0.beta2'
gem 'turbolinks', '2.3.0'
gem 'jbuilder', '2.2.3'
gem 'devise', '3.4.1'
gem 'sdoc', '0.4.0', group: :doc
gem 'pg', '0.17.1'
gem 'bcrypt-ruby', '3.1.1.rc1', :require => 'bcrypt'
group :development, :test do
gem 'byebug', '3.4.0'
gem 'web-console', '2.0.0.beta2'
gem 'spring', '1.1.3'
end
group :production do
gem 'rails_12factor', '0.0.2'
end
group :test do
gem 'minitest-reporters', '1.0.5'
gem 'mini_backtrace', '0.1.3'
gem 'guard-minitest', '2.3.1'
end
正在使用:
- ruby 2.1.4p265(2014-1 0-27修訂48166)x86_64的Linux的]
- 軌4.2.0
- 的Postgres 9.3.5
感謝提供任何幫助。
比爾PS,將所產生的架構:
create_table "bookings", force: :cascade do |t|
t.datetime "pickup"
t.integer "hoursBooked", limit: 4
t.integer "lender_id", limit: 4
t.integer "borrower_id", limit: 4
t.boolean "lenderAccepted", limit: 1
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "equipment_id", limit: 4
end
add_index "bookings", ["borrower_id"], name: "index_bookings_on_borrower_id", using: :btree
add_index "bookings", ["equipment_id"], name: "index_bookings_on_equipment_id", using: :btree
add_index "bookings", ["lender_id"], name: "index_bookings_on_lender_id", using: :btree
我認爲底層的問題有三重:(1)AR/MySQL使用'tinyint'與C風格'1'和'0'爲布爾值,而PostgreSQL有一個本地布爾類型。 (2)MySQL喜歡在整數列定義中包含大小。 (3)AR足夠聰明,可以在'schema.rb'中將'tinyint'轉換回''boolean'',但是忽略大小使得它看起來像'tinyint(1)'從MySQL返回並且包含'schema.rb'中的'limit:1',即使它不應該在那裏。非常適合數據庫的可移植性。 –