magnetised

declaring property 'key' breaks everything

Reported by magnetised | November 7th, 2008 @ 10:24 AM

I've been using a model which has a property of 'key', thus:

property :key, String

All was well until I tried to update the attributes of that particular model or use it in associations (updating would throw an error, belongs_to assocs pointing to the model would fail to link).

It took me a while to realise that the 'key' attribute is used internally by dm to link to the primary key value.

To avoid developer heartache and hairloss, DM should throw an error if someone tries to assign a property with a name (such as 'key') that's going to break dm's internals.

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) December 2nd, 2008 @ 11:59 PM

    • Tag changed from dm-core, property to bug, dm-core, property
    • State changed from “new” to “open”
    • Assigned user cleared.

    When adding any methods dynamically we should always check the target class/module to see if a method already exists by that same name. If one does, then an exception should be thrown. Ideally we should create a method that handles method creation that can centralize the checks and exception throwing.

  • cies

    cies December 22nd, 2008 @ 06:03 AM

    • State cleared.

    sorry duped the bug here: http://datamapper.lighthouseapp....

    i thought it was a dm-is-list issue.

    dkubb: your suggested solution sounds nice.

  • Bright 4

    Bright 4 April 24th, 2009 @ 02:21 AM

    I have found a very much related issue. If one has a property named :key AND another Json property, calling the Json property after getting an object from the db, fails in dm-core-0.9.11/lib/dm-core/model.rb:394, when self.key is called. If the :key property is commented, all works well.

    Indeed, dkubb's solution would be very welcome. I've spent a few hours trying to figure out what was wrong with my side of the code...

    The following reproduces the issue:

    
    require "rubygems"
    require "dm-core"
    require "dm-types"
    
    # Datamapper.Setup here
    
    class Klass
      include DataMapper::Resource
      property :id,     Serial
      property :key,    String, :default => "K"
      property :value,  Json
    end
    
    DataMapper.auto_migrate!
    
    k1 = Klass.new(:value => ["test"])
    k1.save
    
    k2 = Klass.first
    p k2.key
    p k2.value
    

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

Referenced by

Pages