Giter Club home page Giter Club logo

Comments (4)

graimon avatar graimon commented on July 26, 2024 4

Hello,

I know this is a bit old, but as the PR has not been merged yet, I feel like I need to tell my experience with this issue.

Using pomier's solution, seemed to work at first, but when trying to run Lhm.cleanup(true) lot's of Foreign Key references errors were raised.
Looking a little further, it seemed that referenced tables had not been modified correctly.

The only thing that has worked for me is to remove foreign keys explicitly, run the Lhm migration and add foreign keys again inside the change_table body.

remove_foreign_key "fk_example", "users"
Lhm.change_table :fk_example do |m|
      t.ddl "ALTER TABLE %s ADD CONSTRAINT `fk_rails_#{SecureRandom.hex(5)}` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);" % t.name
end

from lhm.

erikogan avatar erikogan commented on July 26, 2024

Also, migrating a table with outbound foreign key constraints fails on the temporary table creation step, because the new table tries to reuse the constraint name (errno: 121):

CREATE TABLE `fk_example` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `user_id` int(11) NOT NULL,
  CONSTRAINT `fk_example_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

and

Lhm.change_table :fk_example do |m|
  # Nothing required, the create fails
end

Produce the following error:

[…]gems/mysql2-0.2.18/lib/active_record/connection_adapters/mysql2_adapter.rb:265:in `query': Mysql2::Error: Can't create table 'change_devel.lhmn_fk_example' (errno: 121): CREATE TABLE `lhmn_fk_example` ( (ActiveRecord::StatementInvalid)
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_example_ibfk_1` (`user_id`),
  CONSTRAINT `fk_example_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 /* large hadron migration */
  from […]gems/mysql2-0.2.18/lib/active_record/connection_adapters/mysql2_adapter.rb:265:in `block in execute'
  from […]gems/activerecord-3.0.20/lib/active_record/connection_adapters/abstract_adapter.rb:202:in `block in log'
  from […]gems/activesupport-3.0.20/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  from […]gems/activerecord-3.0.20/lib/active_record/connection_adapters/abstract_adapter.rb:200:in `log'
  from […]gems/mysql2-0.2.18/lib/active_record/connection_adapters/mysql2_adapter.rb:265:in `execute'
  from […]gems/lhm-1.3.0/lib/lhm/connection.rb:135:in `execute'
  from […]gems/lhm-1.3.0/lib/lhm/connection.rb:92:in `block in sql'
  from […]gems/lhm-1.3.0/lib/lhm/connection.rb:91:in `each'
  from […]gems/lhm-1.3.0/lib/lhm/connection.rb:91:in `sql'
  from […]gems/lhm-1.3.0/lib/lhm/connection.rb:131:in `destination_create'
  from […]gems/lhm-1.3.0/lib/lhm/migrator.rb:167:in `destination_create'
  from […]gems/lhm-1.3.0/lib/lhm/migrator.rb:161:in `execute'
  from […]gems/lhm-1.3.0/lib/lhm/command.rb:17:in `run'
  from […]gems/lhm-1.3.0/lib/lhm/invoker.rb:37:in `run'
  from […]gems/lhm-1.3.0/lib/lhm.rb:40:in `change_table'

(Note that the FK constraint on the new table is still named fk_example_ibfk_1)

from lhm.

Misosooup avatar Misosooup commented on July 26, 2024

If you remove the foreign keys first, won't you get the issue where a cascade delete/update won't happen when the migration is running. Adding foreign key back later in LHM may cause reference error since the it cannot find the desired key to mapped to?

from lhm.

tboyko avatar tboyko commented on July 26, 2024

It seems like LHM should account for foreign keys, no? We got bitten by its lack of support in our dev environment. Luckily it didn't make it to prod...

from lhm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.