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.

Possible bug - Inconsistent results in DM collection arithmetic #263

Open
@crantok

Description

@crantok

I've hit a problem when using collection arithmetic. I'll mention up-front that it involves a 2 step association (i.e. :through => :an_intermediate_table).

I was getting what I felt were incorrect results and then, while debugging, I realised that forcing a collection to load data from the DB (by inspecting the collection) changed the results.

I've tried to shrink my code (below) down to the simplest possible example but it still looks bigger than I'd like. With the code as written, the output I get is:

[#<Issue @id=1 @assigned_to_id=1 @project_id=1>]
[]
[]

And after uncommenting the indicated line (which I expected to have no side-effects) the output I get is:

[#<Issue @id=1 @assigned_to_id=1 @project_id=1>]
[#<Issue @id=2 @assigned_to_id=nil @project_id=1>]
[]

Neither output looks correct to me. I expected to see:

[#<Issue @id=1 @assigned_to_id=1 @project_id=1>]
[#<Issue @id=2 @assigned_to_id=nil @project_id=1>]
[#<Issue @id=3 @assigned_to_id=nil @project_id=1>]

And here's the code:

require 'dm-core'
require 'dm-migrations'

class Issue
  include DataMapper::Resource
  property :id, Serial

  belongs_to :assigned_to, 'User', :required => false
  belongs_to :project, :required => true
  has n, :issue_comments
end

class IssueComment
  include DataMapper::Resource
  property :id, Serial

  belongs_to :issue, :required => true
  belongs_to :user, :required => true
end

class User
  include DataMapper::Resource
  property :id, Serial

  has n, :assigned_issues, 'Issue', :child_key => [ :assigned_to_id ]
  has n, :issue_comments
  has n, :issues, :through => :issue_comments

  def discussed_issues
    # Wanted "has n, :issues" to be "has n, :discussed_issues" but could
    # not figure out whether this was possible.
    issues
  end
end

class Project
  include DataMapper::Resource
  property :id, Serial

 has n, :issues
end


DataMapper.setup(:default,  "sqlite3://#{Dir.pwd}/dm_test.db" )
DataMapper.finalize
DataMapper.auto_upgrade!


if User.count == 0
  user = User.create
  project = Project.create
  issues = [
            Issue.create( :project => project, :assigned_to => user ),
            Issue.create( :project => project ),
            Issue.create( :project => project )
           ]
  comment = IssueComment.create( :issue => issues[1], :user =>user )
end

user = User.all.first
project = Project.all.first


# !!! Uncomment this line to get a different result !!!
# user.discussed_issues.inspect


assigned_issues = user.assigned_issues & Issue.all( :project => project )

discussed_issues =
  ( user.discussed_issues & Issue.all( :project => project ) ) - assigned_issues

other_issues =
  Issue.all( :project => project ) - ( discussed_issues|assigned_issues)


puts assigned_issues.inspect
puts discussed_issues.inspect
puts other_issues.inspect

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