#796 ✓resolved
Marcin Kulik

lazy load error after find_by_sql

Reported by Marcin Kulik | February 4th, 2009 @ 05:16 AM

I have Enum type in my Topic model and after fetching topics with custom sql by find_by_sql and issuing topics.size I get an error "+value+ must be nil or a String" from Yaml type. I've create spec to reproduce it.

require 'rubygems'
require 'spec'
require 'dm-core'
require 'dm-types'

DataMapper.setup(:default, 'sqlite3::memory:')

class Topic
  include DataMapper::Resource

  property :id,           Serial
  property :name,         Yaml, :lazy => false, :default => lambda { Hash.new }
  property :access_level, Enum[:public, :private, :other], :default => :public
  property :foo,          String, :default => "bar"

  def self.all_with_sql(fields)
    self.find_by_sql("SELECT #{fields} FROM topics")


describe Topic do
  before(:all) do
    Topic.create(:name => { :en => "Name", :pl => "Nazwisko"})

  it "should find all topics with only id in field list" do
    topics = Topic.all_with_sql("id")
    topics.size # works

  it "should find all topics with id and access_level in field list" do
    topics = Topic.all_with_sql("id, access_level")
    topics.size # error! 
    # the same with topics.empty?

  it "should find all topics with id and foo in fields list" do
    topics = Topic.all_with_sql("id, foo")
    topics.size # works

Notice that first (only "id") and third case ("id" and String property "foo") work, but second case where I add Enum type field "access_level" to fetched fields doesn't work and raises error from Yaml type.

It's on DM 0.9.10

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) February 21st, 2009 @ 02:47 AM

    • Tag set to enum, find_by_sql, lazy, yaml
    • State changed from “unconfirmed” to “confirmed”

    I can confirm this is a bug. Offhand I do not know what is causing the bug. Will require further study.

  • whoahbot

    whoahbot June 9th, 2009 @ 11:54 PM

    This problem does not occur with 0.10.0 when you change line 18 to read: repository.adapter.query("SELECT #{fields} FROM topics")


  • Marcin Kulik

    Marcin Kulik June 10th, 2009 @ 02:36 AM

    @whoahbot: But find_by_sql does more than repository.adapter.query, it puts results from query into model objects. So this change doesn't proove that this bug is fixed.

  • Dan Kubb (dkubb)
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) June 10th, 2009 @ 12:20 PM

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

    Marcin: yeah, I mentioned that to whoaboat on irc last night. While his example proves that that underlying logic functions, your example still shows there's a problem with how find_by_sql interprets the results and creates the Collection/Resource objects in DM 0.9.

    I just tried this spec with dm-core/next and dm-more/next (find_by_sql was moved to dm-ar-finders, so I require that explicitly) and can confirm it passes. Marking this ticket as resolved and attaching a slightly modified script to demonstrate the fix.

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