tags:

views:

369

answers:

2

I'm just getting started with Subsonic 3.0 ActiveRecord and am trying to implement a batch query like the one in the SubSonic docs. I'm using a batch so I can query a User and a list of the users Orders in one shot.

When I call the BatchQuery.Queue() method, adding my "select user" query, SubSonic throws the following exception:

System.InvalidOperationException : Can't decide which property to consider the Key - you can create one called 'ID' or mark one with SubSonicPrimaryKey attribute

The code is as follows:

var db = new MyDB();
var userQuery = from u in db.Users //gets user by uid
                where u.uid == 1
                select u;

var provider = ProviderFactory.GetProvider();
var batch = new BatchQuery(provider);
batch.Queue(userQuery); //exception here

//create and add "select users orders" query here...

First things first - Why this error? My SubSonic Users object knows it's PK. "uid" is the PK in the database and the generated code reflects this. And I thought SubSonicPrimaryKey attribute was for the SimpleRepository? Is this way of batching not for ActiveRecord?

I could ask a number of other questions, but I'll leave it at that. If anyone can help me figure out what is going on and how to issue 2 batched queries I'd be grateful!

Edit - after further investigation

I ran through the source code with the debugger. Adam is correct - the ToSchemaTable() method in Objects.cs is apparently building out my schema and failing to find a PK. At the very end, it tries to find a column property named "ID" and flags this as the PK, otherwise it throws the exception. I added a check for "UID" and this works!

Still... I'm confused. I'm admittedly a bit lost after peeling back layer after layer of the source, but it seems like this portion of code is trying to build up a schema for my table and completely ignoring my generated User class - which quite nicely identifies which column/property is the PK! It doesn't seem quite right that I'd be required to name all keys "ID" w/ ActiveRecord.

+1  A: 

SubSonic expects your primary key to be called Id so it's getting confused. SubSonicPrimaryKey is for simple repository but I assume where that exception is being thrown is shared between the different templates. If you rename your PK to Id or id or ID your query will work.

Adam
Thanks! This is not the answer I wanted to hear, but you did help me further troubleshoot and bandaid a workaround (see my edit for detail).
Kurt Schindler
Looks like this is going to be fixed though so hopefully you won't need the workaround for long.
Adam
+2  A: 

I think the answer you're looking for is that this is a really stupid bug on my part. I'm hoping to push another build next week and if you could put this on the issue list I'd really appreciate it. My apologies...

Rob Conery
Issue submitted. Thanks for the confirmation on the bug. http://github.com/subsonic/SubSonic-3.0/issues/#issue/81
Kurt Schindler