Cannot do rake db:test:clone. Foreign key constraints applied to test schema in wrong order

19 views
Skip to first unread message

Saint

unread,
Nov 5, 2009, 8:02:41 PM11/5/09
to Oracle enhanced adapter for ActiveRecord
Hi,

I am using Rails 2.2.2 with Ruby 1.8.7, Oracle 11g and version 1.2.2
of the oracle enhanced adapter.

When running rake db:test:clone I get the following error:

rake aborted!
OCIError: ORA-00942: table or view does not exist: ALTER TABLE
category_items ADD CONSTRAINT fk_ci_complaint FOREIGN KEY
(complaint_id) REFERENCES complaints(id)

It looks like the schema.rb statements (which are sorted
alphabetically based on the table name) are run sequentially against
the test database. This results in an attempt to add a foreign key
referencing a yet to be created table.

Here is an extract of the relevant section from the schema.rb file:

===================================================
ActiveRecord::Schema.define(:version => 20091102200623) do

create_table "categories", :force => true do |t|
t.string "name"
end

create_table "category_items", :force => true do |t|
t.integer "complaint_id", :precision => 38, :scale => 0, :null =>
false
t.integer "category_id", :precision => 38, :scale => 0, :null =>
false
end

add_foreign_key "category_items", "categories", :name =>
"fk_ci_category"
add_foreign_key "category_items", "complaints", :name =>
"fk_ci_complaint"

create_table "complaints", :force => true do |t|
t.integer "complaint_number", :precision => 38, :scale =>
0, :null => false
t.datetime "date_received", :null => false
end

=========================================

I do not have this error with version 1.2.1 of the adapter.

Thanks,
Santosh

Raimonds Simanovskis

unread,
Nov 6, 2009, 2:52:23 AM11/6/09
to Oracle enhanced adapter for ActiveRecord
Yes, didn't think about this issue when exporting foreign key
constraints to schema.rb (which is new feature in 1.2.2 version of
adapter).
I created a fix (which moves all foreign key definitions after
creation of all tables) that will be included in next version of
oracle_enhanced adapter.

One option is that you can git clone repository from github.com/rsim/
oracle-enhanced and then run "rake install_gem".

Other option is to include the following monkey patch in your Rails
application which will fix this issue:

module ActiveRecord
module ConnectionAdapters
module OracleEnhancedSchemaDumper
private

def tables_with_oracle_enhanced(stream)
sorted_tables = @connection.tables.sort
sorted_tables.each do |tbl|
# add table prefix or suffix for schema_migrations
next if [ActiveRecord::Migrator.proper_table_name
('schema_migrations'), ignore_tables].flatten.any? do |ignored|
case ignored
when String; tbl == ignored
when Regexp; tbl =~ ignored
else
raise StandardError,
'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String
and / or Regexp values.'
end
end
# change table name inspect method
tbl.extend TableInspect
table(tbl, stream)
# add primary key trigger if table has it
primary_key_trigger(tbl, stream)
end
sorted_tables.each do |tbl|
# add foreign keys if table has them
foreign_keys(tbl, stream)
end
# add synonyms in local schema
synonyms(stream)
end
end
end
end
Reply all
Reply to author
Forward
0 new messages