Skip to content
This repository was archived by the owner on Apr 17, 2018. It is now read-only.
This repository was archived by the owner on Apr 17, 2018. It is now read-only.

dm-constraints does not load models correct to infer key #3

@solnic

Description

@solnic

I don't really know how to describe this bug, except in code:

require 'rubygems'
require 'dm-core'
require 'dm-migrations'
require 'dm-constraints'

DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'postgres://localhost/test') # createdb test

class Post
  include DataMapper::Resource

  property :id, Serial
  belongs_to :author
end

class User
  include DataMapper::Resource

  property :id, Serial
end

class Author
  include DataMapper::Resource

  belongs_to :user, :key => true, :child_key => [:id]

  has n, :posts
end


DataMapper.finalize
DataMapper.auto_migrate!

Fails with:

 ~ (0.000255) ALTER TABLE "posts" ADD CONSTRAINT "posts_author_fk" FOREIGN KEY () REFERENCES "authors" () ON DELETE NO ACTION ON UPDATE NO ACTION
 ~ ERROR:  syntax error at or near ")"
LINE 1: ...ts" ADD CONSTRAINT "posts_author_fk" FOREIGN KEY () REFERENC...
                                                             ^
 (code: 16801924, sql state: 42601, query: ALTER TABLE "posts" ADD CONSTRAINT "posts_author_fk" FOREIGN KEY () REFERENCES "authors" () ON DELETE NO ACTION ON UPDATE NO ACTION, uri: postgres://localhost/test)
/Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-do-adapter-1.0.2/lib/dm-do-adapter/adapter.rb:63:in `execute_non_query': ERROR:  syntax error at or near ")" (DataObjects::SyntaxError)
LINE 1: ...ts" ADD CONSTRAINT "posts_author_fk" FOREIGN KEY () REFERENC...
                                                             ^
    from /Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-do-adapter-1.0.2/lib/dm-do-adapter/adapter.rb:63:in `block in execute'
    from /Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-do-adapter-1.0.2/lib/dm-do-adapter/adapter.rb:260:in `with_connection'
    from /Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-do-adapter-1.0.2/lib/dm-do-adapter/adapter.rb:61:in `execute'
    from /Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-constraints-1.0.2/lib/dm-constraints/adapters/dm-do-adapter.rb:64:in `create_relationship_constraint'
    from /Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-constraints-1.0.2/lib/dm-constraints/migrations.rb:51:in `block in execute_each_relationship'
    from /Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-constraints-1.0.2/lib/dm-constraints/migrations.rb:50:in `each_value'
    from /Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-constraints-1.0.2/lib/dm-constraints/migrations.rb:50:in `execute_each_relationship'
    from /Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-constraints-1.0.2/lib/dm-constraints/migrations.rb:43:in `auto_migrate_up_constraints!'
    from /Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-migrations-1.0.2/lib/dm-migrations/auto_migration.rb:45:in `block in repository_execute'
    from /Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-core-1.0.2/lib/dm-core/support/descendant_set.rb:68:in `block in each'
    from /Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-core-1.0.2/lib/dm-core/support/descendant_set.rb:67:in `each'
    from /Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-core-1.0.2/lib/dm-core/support/descendant_set.rb:67:in `each'
    from /Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-migrations-1.0.2/lib/dm-migrations/auto_migration.rb:44:in `repository_execute'
    from /Users/xavier/.rvm/gems/ruby-1.9.2-p0/gems/dm-constraints-1.0.2/lib/dm-constraints/migrations.rb:12:in `auto_migrate!'
    from dm-load-order.rb:32:in `<main>'

The work around is to move the Post definition below the Author definition (or if using autoloading and rails, add a reference to Author at the top of the post.rb file to force it to be loaded before the rest of the file is run)


Created by Xavier Shay - 2010-12-16 02:58:14 UTC

Original Lighthouse ticket: http://datamapper.lighthouseapp.com/projects/20609/tickets/1457

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions