#1287 ✓resolved

auto_migrate! doesn't work with :through => Resource

Reported by pschuegr | May 22nd, 2010 @ 12:34 PM | in 1.0.0

When using the anonymous joins (:through => Resource) auto_migrate! seems to have trouble dropping tables because of the constraints.

The sql error looks like this:

ERROR: cannot drop table users because other objects depend on it
DETAIL: constraint artist_users_owner_fk on table artist_users depends on table users
constraint site_users_owner_fk on table site_users depends on table users
HINT: Use DROP ... CASCADE to drop the dependent objects too.

See here:

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 23rd, 2010 @ 08:47 PM

    • State changed from “new” to “unconfirmed”

    @pschueger: Are you using DataMapper.auto_migrate! or are you trying to migrate on a per-model basis. Doing it on a per-model basis will not work when you are using dm-constraints, since auto-migrations need to happen in 2 phases:

    • Migrating down

      1. Remove all constraints across all tables
      2. Remove all tables
    • Migrating up

      1. Add all tables
      2. Add all constraints

    If you try to migrate on a per-model basis, it'll drop the constraints for that table, but nothing will happen to any table that references it. For that you must use DataMapper.auto_migrate! since it acts on all models.

    If it is the case you are using DataMapper.auto_migrate!, are you able to reproduce the problem in a stand-alone script?

  • pschuegr

    pschuegr May 24th, 2010 @ 08:52 PM

    Using DataMapper.auto_migrate!

    class Model1
      include DataMapper::Resource
      property :id, Serial
      has n, :model2, 'Model2', :through => Resource
    class Model2
      include DataMapper::Resource
      property :id, Serial
      has n, :model1, 'Model1', :through => Resource

    Create brand new rails app with these two models, do a db:create, db:setup, and then do DataMapper.auto_migrate! in the console.

    I'm using postgresql but I believe somebody on the dm irc said it also fails with mysql.

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 26th, 2010 @ 01:03 AM

    • Assigned user set to “Jonathan Stott (namelessjon)”

    Attached is a stand-alone script that demonstrates the expected behaviour, so I think that the problem may be something related to how these models are setup in Rails 3. (I only assume Rails 3, if it's something else please let me know)

    As a test, can you perform the auto-migration from the console, but just before you do DataMapper.auto_migrate! can you run the following command:

    DataMapper::Model.descendants.each { |model| model.send(:assert_valid) }

    Please let me know if this works. Assuming for a moment that it does, the problem you are running into is that when Rails (re)loads the models sometimes all of the associations are not setup properly. In the near future (within the next day or so), we'll be adding a method DataMapper.finalize that performs this and other setup tasks. dm-rails will be updated to perform this properly so you shouldn't have to ever use it directly (or type in the line above).

    I am assigning to Johnathan Stott (namelessjon on IRC) who is working on DataMapper.finalize.

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 26th, 2010 @ 02:04 AM

    • Milestone set to 1.0.0
  • pschuegr

    pschuegr May 26th, 2010 @ 03:10 AM

    Suggested fix works

  • Jonathan Stott (namelessjon)

    Jonathan Stott (namelessjon) May 26th, 2010 @ 09:28 AM

    • Tag cleared.
    • State changed from “unconfirmed” to “accepted”

    I've just confirmed that DataMapper.finalize does solve this issue, too.

  • Jonathan Stott (namelessjon)

    Jonathan Stott (namelessjon) May 27th, 2010 @ 05:05 PM

    • State changed from “accepted” to “resolved”

    Marking this as resolved.

    Calling DataMapper.finalize before DataMapper.auto_migrate! resolves the issue. Please reopen if you find differently.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »