#125 ✓resolved
Kevin Bullock

Property declarations should raise for unsupported types

Reported by Kevin Bullock | January 4th, 2008 @ 10:16 AM


I've been playing around with Merb+DataMapper to create a cheapo little blog, and ran into the following error while loading a Post (which has a created_at) up from the database:

>> Post.new(:title => 'Foo', :text => 'Bar').save
=> true
>> Post.first
DataMapper::MaterializationError: Failed to materialize column :created_at with value "2008-01-04 10:03:37"
undefined method `type_cast_timestamp' for #<DataMapper::Adapters::MysqlAdapter:0x18832f8>
	(eval):2:in `type_cast_value'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:62:in `materialize'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:54:in `each_pair'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:54:in `materialize'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:248:in `load'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:247:in `each_pair'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:247:in `load'
	/Library/Ruby/Gems/1.8/gems/do_mysql-0.2.3/lib/do_mysql.rb:132:in `each'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:246:in `load'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:223:in `call'
	/Library/Ruby/Gems/1.8/gems/do_mysql-0.2.3/lib/do_mysql.rb:180:in `execute_reader'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:219:in `call'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/data_object_adapter.rb:77:in `connection'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/support/connection_pool.rb:67:in `hold'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/data_object_adapter.rb:77:in `connection'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:216:in `call'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/data_object_adapter.rb:321:in `load'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/context.rb:44:in `first'
	/Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/support/active_record_impersonation.rb:53:in `first'
	(irb):15:in `irb_binding'
	/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:65:in `materialize'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:54:in `each_pair'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:54:in `materialize'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:248:in `load'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:247:in `each_pair'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:247:in `load'
	from /Library/Ruby/Gems/1.8/gems/do_mysql-0.2.3/lib/do_mysql.rb:132:in `each'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:246:in `load'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:223:in `call'
	from /Library/Ruby/Gems/1.8/gems/do_mysql-0.2.3/lib/do_mysql.rb:180:in `execute_reader'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:219:in `call'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/data_object_adapter.rb:77:in `connection'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/support/connection_pool.rb:67:in `hold'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/data_object_adapter.rb:77:in `connection'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:216:in `call'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/data_object_adapter.rb:321:in `load'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/context.rb:44:in `first'
	from /Library/Ruby/Gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/support/active_record_impersonation.rb:53:in `first'
	from (irb):15>> 

As you can see, the model saves fine, but fails to load back up. Here's the model:

class Post < DataMapper::Base
  belongs_to :section
  has_many :comments

  property :title, :string
  property :text, :text
  property :created_at, :timestamp

  validates_length_of :title
  validates_presence_of :text

Pretty standard, as you can see.

I also can't set the created_at property on an object. No exception is raised, it just silently fails. Is timestamp column support just not yet implemented?

I'm using DataObjects.rb, DataMapper, and Merb all from svn trunk (DO.rb from rubyforge.org svn).

Comments and changes to this ticket

  • Sam Smoot

    Sam Smoot January 4th, 2008 @ 05:51 PM

    • Milestone cleared.
    • State changed from “new” to “open”
    • Title changed from “Can't load object with timestamp column” to “Property declarations should raise for unsupported types”

    It's :datetime, not :timestamp.

    But this does raise an important point... property declarations should raise errors for unsupported types. As well as probably telling you what the supported types are for convenience.

  • Sam Smoot

    Sam Smoot January 4th, 2008 @ 05:52 PM

    PS: This should probably happen (the raising for invalid types) in Property#initialize or something.

  • Kevin Bullock

    Kevin Bullock January 4th, 2008 @ 08:03 PM

    I was aware of the :datetime type. I had thought, though, that there would be support for the TIMESTAMP type in MySQL, which is separate from DATETIME. If that's not the case (it has dubious and ever-changing semantics anyway), I'll work on a patch to raise an error.

  • Kevin Bullock

    Kevin Bullock January 4th, 2008 @ 09:19 PM

    Got a patch here. Any cleanup needed?

  • Sam Smoot

    Sam Smoot January 5th, 2008 @ 01:19 AM

    Patch looks good to me. The only tweak I might make is to use #has_key? instead of #[] lookup.

    Kevin: So what's the difference between datetime and timestamp anyways? Is it significant enough to warrant support?

  • Sam Smoot

    Sam Smoot January 5th, 2008 @ 01:20 AM

    Oh yeah, and a corresponding spec in property_spec.rb would be nice. ;-)

    You can search for project for "raise_error" to find an example of how to test for an error with RSpec.

  • Kevin Bullock

    Kevin Bullock January 5th, 2008 @ 09:28 AM

    Yeah, I spent some time learning the basics of RSpec last night in the interest of writing a spec :) I'll try to post one later today.

    In MySQL, the DATETIME type is the more general one, and seems equivalent to PostgreSQL's timestamp type. The MySQL TIMESTAMP is basically equivalent to a Unix timestamp, i.e. the range is from 1970-01-01 00:00:00 to sometime in 2038, when 32-bit integer overflow occurs.

    A TIMESTAMP column can also be auto-set on insert and update to the current time. This gets at the same functionality as the magic properties (created_at, etc.), so I'm not sure if DataMapper really needs to support it.

    More detail here:


    and especially here:


  • Kevin Bullock

    Kevin Bullock January 5th, 2008 @ 11:50 AM

    Okay, got an updated patch here, with spec. I didn't know you could actually define a class within a lambda, but it works!

  • Kevin Bullock

    Kevin Bullock January 5th, 2008 @ 11:55 AM

    Whoops, one more version of the patch, this time switched to #has_key? instead of #[]. Attaching unsupported_types_2.patch.

  • Bernerd Schaefer

    Bernerd Schaefer January 6th, 2008 @ 09:29 AM

    • State changed from “open” to “resolved”

    I applied the patch. Thanks Kevin!

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