#847 ✓resolved
Sindre Aarsaether

Faulty associations m:1, fetching 'random' parent

Reported by Sindre Aarsaether | May 8th, 2009 @ 12:26 PM

See the gist below that reproduces the error. A m:1 assoc is seemingly set (to the first parent in the db), even though the child-key is nil, and it has never been associated with this/any parent.. Might very possibly be a duplicate of #846, or atleast a result of a common underlying problem.



require 'rubygems'
require 'dm-core'
 
DataMapper.setup(:default,
  :adapter => 'mysql',
  :host => 'localhost', 
  :username => 'root',
  :database => 'dm_core_test',
  :encoding => 'utf8'
)
 
DataObjects::Mysql.logger = DataObjects::Logger.new(STDOUT, :debug)
 
class VatRate
  include DataMapper::Resource
  
  property :id,   Serial
  property :name, String
  property :rate, BigDecimal
end
 
class Record
  include DataMapper::Resource
  
  property :id, Serial
  property :title,      String
  property :date,       Date
  
  has n, :postings
end
 
class Posting
  include DataMapper::Resource
  
  property :id, Serial
  property :date, Date
  property :amount, BigDecimal, :scale => 2, :precision => 10
 
  belongs_to :vat_rate
  belongs_to :record
  
  before :save do
    puts self.vat_rate_id.inspect # nil
    puts self.vat_rate.inspect # <VatRate @id=1 @name="25% Incoming VAT" @rate=nil>
  end
end
 
VatRate.auto_migrate!
Record.auto_migrate!
Posting.auto_migrate!
 
VatRate.create(:name => "25% Incoming VAT")
 
r = Record.create(:title => "This is a record")
 
r.postings.create(:amount => 100.00)
# As you can see from output, self.vat_rate is set
# inside create block, even though vat_rate_id is nil
# nil
# Thu, 07 May 2009 18:16:00 GMT ~ debug ~ (0.000272) SELECT "id", "name", "rate" FROM "vat_rates" ORDER BY "id" LIMIT 1
# <VatRate @id=1 @name="25% Incoming VAT" @rate=nil>
 
 
puts r.postings.first.inspect
# Thu, 07 May 2009 18:22:32 GMT ~ debug ~ (0.000352) SELECT "id", "date", "amount", "record_id", "vat_rate_id" FROM "postings" WHERE "record_id" = 1 ORDER BY "id" LIMIT 1
#<Posting @id=1 @date=nil @amount=#<BigDecimal:111b1b4,'0.1E3',4(8)> @record_id=1 @vat_rate_id=nil>
 
puts r.postings.first.vat_rate.inspect 
# Thu, 07 May 2009 18:22:32 GMT ~ debug ~ (0.000078) SELECT "id", "date", "amount", "record_id", "vat_rate_id" FROM "postings" WHERE "record_id" = 1 ORDER BY "id" LIMIT 1
#<VatRate @id=1 @name="25% Incoming VAT" @rate=nil>
 
puts Posting.first.vat_rate.inspect
# Thu, 07 May 2009 18:23:43 GMT ~ debug ~ (0.000185) SELECT "id", "date", "amount", "record_id", "vat_rate_id" FROM "postings" ORDER BY "id" LIMIT 1
# CHECK LAST PART: Thu, 07 May 2009 18:23:43 GMT ~ debug ~ (0.000151) SELECT "id", "name", "rate" FROM "vat_rates" WHERE "id" IS NULL
# nil

Comments and changes to this ticket

  • Sindre Aarsaether

    Sindre Aarsaether May 9th, 2009 @ 01:54 PM

    Bug only happens in dkubb/dm-core/sel..

    But next-branch also produces queries like: Sat, 09 May 2009 18:53:25 GMT ~ debug ~ (0.000208) SELECT "id", "name", "rate" FROM "vat_rates" WHERE "id" IS NULL when trying to get the m:1 parent for a resource with no relation.

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 21st, 2009 @ 03:54 PM

    • Assigned user set to “Dan Kubb (dkubb)”
    • State changed from “unconfirmed” to “resolved”

    I've merged SEL into dm-core/next and I believe I've resolved this problem.

    I am marking this as resolved for now, but could you confirm it's fixed and if it isn't I'll re-open it.

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 »

People watching this ticket

Pages