"enum"型の属性 "original_price_period"をマイリストモデルに追加します。このため私はPostgreSQL enum typeとActiveRecord::Enumを組み合わせました。新しく作成されたenum属性がPG :: UndefinedTableをスローする:ERROR:relation "table_name"が存在しません
まず私は、リストのテーブルの上に次の移行を追加しました:
class AddRentableAndOriginalPricePeriodToListings < ActiveRecord::Migration
def up
execute <<-SQL
CREATE TYPE original_price_period AS ENUM ('day', 'week', 'month');
SQL
add_column :listings, :original_price_period, :original_price_period
end
def down
remove_column :listings, :original_price_period
execute <<-SQL
DROP TYPE original_price_period;
SQL
end
end
をその後、私はモデルをリスト最近作成したenumフィールドの宣言を追加しました:
class Listing < ActiveRecord::Base
...
enum original_price_period: {
day: 'day',
week: 'week',
month: 'month'
}
...
end
listing.rb明らかにうまくいった。しかし、私はユニットテストを実行しようとしたとき、それらはすべて、この同じメッセージで破壊された:私はファブリケーターを使用してい
ActiveRecord::StatementInvalid:
PG::UndefinedTable: ERROR: relation "listings" does not exist
LINE 5: WHERE a.attrelid = '"listings"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"listings"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
# ./spec/models/listing_spec.rb:52:in `block (2 levels) in <top (required)>'
# ./spec/models/listing_spec.rb:602:in `block (4 levels) in <top (required)>'
# ./spec/support/database_cleaner.rb:9:in `block (3 levels) in <top (required)>'
# ./spec/support/database_cleaner.rb:8:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# PG::UndefinedTable:
# ERROR: relation "listings" does not exist
# LINE 5: WHERE a.attrelid = '"listings"'::regclass
# ^
# ./spec/models/listing_spec.rb:52:in `block (2 levels) in <top (required)>'
、そしてそれは、彼らが移行前に実行されており、これはするためのテストを引き起こしている可能ですどこかで読ん失敗します。それでも、私はそれを修正する方法を理解することはできません。
rake db:migrate RAILS_ENV=test
rake db:test:prepare
rake db:test:load
私は本当に、このような提案を避けてください、リセットまたは削除し、自分のデータベースを作成したくない:これらは私が失敗したこれまでにしようとしているものです。それが関係している可能性があるので、こちらもspec_helper.rbを追加:
spec_helper.rb
require 'simplecov'
require 'simplecov-json'
require 'simplecov-rcov'
require 'fantaskspec'
SimpleCov.formatters = [
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::JSONFormatter,
SimpleCov::Formatter::RcovFormatter
]
SimpleCov.start
require 'webmock/rspec'
RSpec.configure do |config|
# this fixes backtraces for me, it can be removed once this change is shipped: https://github.com/rspec/rspec-core/pull/1616
config.backtrace_exclusion_patterns = [
/(?-mix:(?-mix:\/lib\/rspec\/(core|mocks|expectations|support|matchers|rails|autorun)(\.rb|\/))|rubygems\/core_ext\/kernel_require\.rb)|(?-mix:\/libd*\/ruby\/)|(?-mix:org\/jruby\/)|(?-mix:\/bin\/)|(?-mix:\/gems\/)/
]
config.infer_rake_task_specs_from_file_location!
config.mock_with :rspec do |mocks|
mocks.allow_message_expectations_on_nil = true
end
end
UPDATE
どうやら移行が失敗すると、結果として表「リスト」が作成されていませんさ。 PGの列挙は、ネイティブレールでサポートされていませんので、あなたのスキーマがダンプされ、したがって、あなたのテスト・データベースを作成するために使用することはできません
# Could not dump table "listings" because of following StandardError
# Unknown type 'original_price_period' for column 'original_price_period'
あなたのスキーマはどのように見えますか?このエラーメッセージは、リスティングテーブルがテストデータベースに存在しないと考えています。 –
良いキャッチ@ダニエル、それは "リスト"テーブルを作成していません。代わりに、私はスキーマで次の行を取得します: #次のStandardErrorのためにテーブル "listing"をダンプできません # 'original_price_period'列の 'original_price_period'が不明です – ntonnelier