Why am I receiving an ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR when using a boolean field in a rails app with Postgresql?
Why am I receiving an ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR when using a boolean field in a rails app with Postgresql?
我正在开发 rails API 并使用 Heroku。所有环境都使用 Postgres。我在 rake db:setup 和 rake db:test:prepare (以及 'heroku run rake db:setup' 中遇到了同样的错误)。这个问题最初是在我第一次尝试推送到 Heroku 时出现的。我在本地使用 mysql。由于已将开发和测试迁移到 postgres 以避免将来出现此类意外...
$ 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-10-27 修订版 48166)[x86_64-linux]
- rails 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
胜利是亩太短了!感谢您富有洞察力的评论!
看来问题是原来的db:migration 运行 当我使用mysql 时postgres 不能接受。注释掉迁移和架构中的字段,然后将它们重新添加到新的迁移下修复所有...
class AddBackBooleansMigration < ActiveRecord::Migration
def change
add_column :bookings, :lender_accepted, :boolean
# other similar boolean adds
end
end
新架构:
create_table "bookings", force: :cascade do |t|
...
t.boolean "lender_accepted"
end
然后耙它直到你成功。
$ rake db:migrate
== 20150105131615 AddBackBooleansMigration: migrating =========================
-- add_column(:bookings, :lender_accepted, :boolean)
-> 0.0333s
...
== 20150105131615 AddBackBooleansMigration: migrated (0.0370s) ================
由于 AR+mysql 伪造布尔字段的方式,这是 Rails 中的错误。
我为此做了一个补丁:) https://github.com/rails/rails/issues/19065
我正在开发 rails API 并使用 Heroku。所有环境都使用 Postgres。我在 rake db:setup 和 rake db:test:prepare (以及 'heroku run rake db:setup' 中遇到了同样的错误)。这个问题最初是在我第一次尝试推送到 Heroku 时出现的。我在本地使用 mysql。由于已将开发和测试迁移到 postgres 以避免将来出现此类意外...
$ 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-10-27 修订版 48166)[x86_64-linux]
- rails 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
胜利是亩太短了!感谢您富有洞察力的评论!
看来问题是原来的db:migration 运行 当我使用mysql 时postgres 不能接受。注释掉迁移和架构中的字段,然后将它们重新添加到新的迁移下修复所有...
class AddBackBooleansMigration < ActiveRecord::Migration
def change
add_column :bookings, :lender_accepted, :boolean
# other similar boolean adds
end
end
新架构:
create_table "bookings", force: :cascade do |t|
...
t.boolean "lender_accepted"
end
然后耙它直到你成功。
$ rake db:migrate
== 20150105131615 AddBackBooleansMigration: migrating =========================
-- add_column(:bookings, :lender_accepted, :boolean)
-> 0.0333s
...
== 20150105131615 AddBackBooleansMigration: migrated (0.0370s) ================
由于 AR+mysql 伪造布尔字段的方式,这是 Rails 中的错误。 我为此做了一个补丁:) https://github.com/rails/rails/issues/19065