To make it work I selected a column for the primary key as you
suggested, and that worked thanks.  However, my selection of a
primary_key column could be arbitrary as there is no real primary key
in my SELECT statement.

When defining a table, I can define a foreign key column.  Then when
creating the mapper, I define a relation().  This allows my object to
have an attribute that actually references an object, and all is
good.  I am able to query my objects based on criteria for their
children objects like so:

class Shipment
 - date
 - job (Job object)

query(Shipment).filter(Job.number==12345).all()
... gets me all shipments where the job number is 12345

When using a select statement in my mapper instead of a table, how do
I tell SA that a certain field in the SELECT is a foreign key?  Is it
possible to set up a relation in a mapper that is based on a SELECT
statement.  When I did it, it seemed to join my SELECT statement with
the job table as a Cartesian product, because it didn't understand how
to join my SELECT statement with the job table because there were no
foreign keys defined.



On Sep 14, 3:00 pm, "Michael Bayer" <mike...@zzzcomputing.com> wrote:
> Bryan wrote:
>
> > I want to abstract some ugly reporting SQL strings into a read-only
> > object model.  I have created an empty class, and then I map it to a
> > select object that pulls some statistical information from the DB.
> > The mapper is complaining that it can't assemble a primary key.  I am
> > only using this object as a simplified way of querying the database,
> > and will never want to persist the object.  The object is read-only.
> > Is there a way to tell sqlalchemy not to worry about persisting this
> > class?
>
> the primary key is for more than just persistence.  Pick whatever columns
> on your select object you think are suitable, then configure them on the
> mapper using the "primary_key" option:
>
> mapper(MyClass, myselect, primary_key=[myselect.c.foo, myselect.c.bar])
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to