Hola,
Precisamente he estado peleándome con los tres plugins de redhill
estos dias (versiones para rails 1.1.6):
redhillonrails_core
foreign_key_associations
foreign_key_migrations
Dada la siguiente migración:
class AddForeignKeys < ActiveRecord::Migration
def self.up
add_column :systems, :zone_id, :string
add_column :ips, :zone_id, :string
add_column :ips, :mac_id, :string
add_column :macs, :system_id, :string
add_column :macs, :port_id, :string
add_column :ports, :system_id, :string
=begin
add_foreign_key :systems, :zone_id, :zones, :id, :on_delete =>
:set_null, :on_update => :cascade
add_foreign_key :ips, :zone_id, :zones, :id, :on_delete => :set_null,
:on_update => :cascade
add_foreign_key :ips, :mac_id, :macs, :id, :on_delete => :set_null,
:on_update => :cascade
add_foreign_key :macs, :system_id, :systems, :id, :on_delete =>
:set_null, :on_update => :cascade
add_foreign_key :macs, :port_id, :ports_id, :id, :on_delete =>
:set_null, :on_update => :cascade
add_foreign_key :ports, :system_id, :systems, :id, :on_delete =>
:set_null, :on_update => :cascade
=end
end
def self.down
remove_column :systems, :zone_id
remove_column :ips, :zone_id
remove_column :ips, :mac_id
remove_column :macs, :system_id
remove_column :macs, :port_id
remove_column :ports, :system_id
end
end
Me falla al intentar establecer la FK (añade bien la columna
previamente):
$ rake migrate
(in /home/brainstorm/uni/PFC/rubynac/railsnac)
== AddForeignKeys: migrating
– add_column(:systems, :zone_id, :string)
rake aborted!
Mysql::Error: Can’t create table
‘./railsnac_development/#sql-3632_2c.frm’ (errno: 150): ALTER TABLE
systems ADD FOREIGN KEY (zone_id) REFERENCES zones (id)
(See full trace by running task with --trace)
(En este punto, en la BBDD se ha creado zone_id… la migration no
deberia hacer rollback si ha fallado en un punto intermedio :-? (en la
primera instrucción en este caso))
Yo creo que el schema.db está “correcto” :-S:
ActiveRecord::Schema.define(:version => 6) do
create_table “ips”, :force => true do |t|
t.column “ip”, :string
end
create_table “macs”, :force => true do |t|
t.column “mac”, :string
t.column “oid”, :string
end
create_table “ports”, :force => true do |t|
t.column “location”, :string
t.column “portid”, :string
end
create_table “systems”, :force => true do |t|
t.column “oid”, :string
t.column “username”, :string
end
create_table “zones”, :force => true do |t|
t.column “oid”, :string
t.column “name”, :string
t.column “active”, :string
t.column “hostmaster”, :string
t.column “serial”, :string
t.column “serial_date”, :string
t.column “type”, :string
end
end
Alguna idea de pq falla ? Parte de --trace al hacer migrate:
usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract_adapter.rb:120:in
log' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/mysql_adapter.rb:184:in
execute’
/home/brainstorm/uni/PFC/rubynac/railsnac/config/…/vendor/plugins/redhillonrails_core/lib/red_hill_consulting/core.rb:56:in
add_foreign_key' /home/brainstorm/uni/PFC/rubynac/railsnac/config/../vendor/plugins/foreign_key_migrations/lib/red_hill_consulting/foreign_key_migrations.rb:30:in
add_column’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/migration.rb:273:in
send' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/migration.rb:273:in
method_missing’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/migration.rb:257:in
say_with_time' /usr/lib/ruby/1.8/benchmark.rb:293:in
measure’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/migration.rb:257:in
say_with_time' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/migration.rb:271:in
method_missing’
./db/migrate//007_add_foreign_keys.rb:4:in real_up' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/migration.rb:210:in
send’
Saludos & thx !