
missing set_primary_key method
Reported by alan | January 22nd, 2008 @ 04:59 AM
Unfortunately, I can't figure out a simple way to use DataMapper with an old proprietary db, which doesn't follow the protocol of naming the primary key as, "id." I dumped the methods on DataMapper::Base, and while I do see a shiny set_table_name method (as per ActiveRecord's usage), I don't see a method by the name of, "set_primary_key," or anything similar in verbiage. I tried manually defining all columns, and DataMapper still attempts to find an, "id," column, ahead of the rest which I've defined.
Example:
class User < DataMapper::Base
set_table_name 'users' property :id, :integer, :column =>
'pk_user_id' property :company_id, :integer, :column =>
'fk_company_id' end
User.find(:first) DataObject::QueryError: Your query failed.
ERROR: column "id" does not exist
LINE 1: SELECT "id", "pk_user_id", "fk_company_id" FROM "users" LIMI...
^ QUERY: "SELECT "id", "pk_user_id", "fk_company_id" FROM "users" LIMIT 1"
from /opt/local/lib/ruby/gems/1.8/gems/do_postgres-0.2.3/lib/do_postgres.rb:162:inexecute_reader' from /opt/local/lib/ruby/gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:211:in
call' from /opt/local/lib/ruby/gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/data_object_adapter.rb:77:inconnection' from /opt/local/lib/ruby/gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/support/connection_pool.rb:68:in
hold' from /opt/local/lib/ruby/gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/data_object_adapter.rb:77:inconnection' from /opt/local/lib/ruby/gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/sql/commands/load_command.rb:208:in
call' from /opt/local/lib/ruby/gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/adapters/data_object_adapter.rb:295:inload' from /opt/local/lib/ruby/gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/context.rb:44:in
first' from /opt/local/lib/ruby/gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/support/active_record_impersonation.rb:51:infirst' from /opt/local/lib/ruby/gems/1.8/gems/datamapper-0.2.5/lib/data_mapper/support/active_record_impersonation.rb:68:in
find' from (irb):17
Comments and changes to this ticket
-
Adam French January 26th, 2008 @ 01:31 PM
- State changed from new to open
you can do:
property :pk_user_id, :integer, :key => true
and that should emulate set_primary_key. If it doesn't, hit me back.
-
-
-
Michael Boutros February 8th, 2008 @ 10:24 PM
Do you guys think that there should be a 'set_primary_key' method, since: a) there is a 'set_table_name' method to do the same thing for tables, and more compellingly, b) it'll be easier to document. Where in the docs could it be mentioned that this is how to change the primary key? It'd be thrown somewhere randomly, and for people just searching the docs they wouldn't find it. I'm sure it wouldn't be too hard to implement, I'll whip up a patch tomorrow.
-
asceth February 9th, 2008 @ 01:11 AM
Unless the patch works directly with the property method I don't see how this would be beneficial and even then it sounds like it would be a bit of cleverness over what should be simple. #set_table_name only exists because there is no other way to define the table name.
#property is meant to define columns and has the ability to define the primary keys. Adding #set_primary_key would be duplication of functionality and would need to go through the properties defined and update that one.
In documentation you can update it under DataMapper::Persistence to point to where it should be defined. This could also open up a intro/tutorial on how to support legacy databases on the website.
These are just my views so don't let me turn you off on creating the patch.
-
Adam French February 11th, 2008 @ 10:22 AM
I've added a section in the new FAQ to cover this. I want to say there's even a section on the Property documentation which covers Keys as well.
-
Michael Boutros February 11th, 2008 @ 02:59 PM
Yep, there is a paragraph in Property's documentation about adding keys, and primary keys in legacy databases is covered.
-
Sam Smoot February 11th, 2008 @ 08:41 PM
Alan:
A patch would be great, but I think we want to add that specifically to a ActiveRecordCompatibility module that can be optionally included.
Don't want to litter the core namespace with compatibility methods regular users aren't likely to use anyways right?
I don't think it's terribly important where this file goes... AR users who want it can just require & include it to decorate the DM namespace.
-
Michael Boutros February 11th, 2008 @ 09:57 PM
Alan, feel free to create a new ticket for your problem. No one will get mad at you for trying to help out :) If you want to really one-on-one with Sam and the other big-timers, you can usually find them in the IRC channel: #datamapper on irc.freenode.net. If for some reason you can never catch them online, creating a ticket will definitely get their attention.
-
Sam Smoot February 12th, 2008 @ 07:27 PM
Alan, you can find my email addy through the mailing-list. I'd paste it here, but you know... :-)
http://groups.google.com/group/d...
We've been over RETURNING before, but I forget why we rejected it... I think maybe it was just because we would need to inspect the query if we did it at the driver level? Not sure. Be happy to chat about it sometime.
Hate doing too much on email/IRC tho'. I'm just lazy like that. ;-) So if you've got time for an ichat sometime...
Of course, as Michael suggested, I'm on IRC pretty frequently, so feel free to drop in and ping me.
-
Michael Boutros February 12th, 2008 @ 09:24 PM
Sam, I've been working on adding this and I've got it working:
class Article < DataMapper::Base include_compatibility_methods set_primary_key 'title', :string property :description, :string end
Is that good for everyone? We could also add more methods to the Compatibility module at a later date.
-
Michael Boutros February 12th, 2008 @ 11:00 PM
I want to add a patch, but I'm having some trouble - how can I get git to add to the diff file the new file (compatibility.rb) that I created?
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 »