
Problem with associations when types don't match
Reported by Carl Porth | December 12th, 2009 @ 04:48 PM
Association doesn't work when foreign key type doesn't match
primary key type. (I think a typecasting issue.)
This feature is especially desirable when using cross repository associations.
Comments and changes to this ticket
-
Carl Porth December 18th, 2009 @ 01:27 PM
To clarify further:
Let's say I'm using a 3rd party library which provides an adapter and some default models for a webservice for listing products (like google products). The webservice calls for a product_id with type String. On my db, I have on my own product model with product_id defined as an Integer. I would like to associate the two.
It might look something like this:
require 'rubygems' gem 'dm-core', '0.10.2' gem 'do_sqlite3', '0.10.0' require 'dm-core' module Webservice class Product include DataMapper::Resource def self.default_repository_name :webservice end property :id, String, :key => true property :name, String property :product_id, String belongs_to :product, 'Store::Product', :repository => :default end end module Store class Product include DataMapper::Resource property :id, Serial property :name, String has 1, :webservice_product, 'Webservice::Product', :repository => :webservice end end DataMapper.setup(:default, 'sqlite3::memory:') DataMapper.setup(:webservice, 'sqlite3::memory:') DataMapper.auto_migrate! Webservice::Product.create(:id => 'x', :name => 'foo', :product => Store::Product.create(:name => 'foo')) # in 0.10.1: Webservice::Product.first.product # => #<Store::Product @id=1 @name="foo"> # in 0.10.2: Webservice::Product.first.product # => nil
In 0.10.1, this type of association worked.
In 0.10.2, this no longer works.I'm not sure it is a regression because the behavior is undefined.
Is this a weird edge case not worth handling?
-
Dan Kubb (dkubb) February 5th, 2010 @ 02:13 AM
- State changed from new to not-applicable
Actually, in the future it will only be possible to join on properties that have the same type and the same constraints (like :min/:max for Integer, :length for String, etc). I recently reverted a commit that did implicit coercion between types in a join.
I may add a way to explicitly coerce the types to another before joining them, but that will likely be after DM 1.0, and after we rework the query/adapter API to be built off veritas or something similar.
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.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »