Two Table Entity In Declarative Style

1 view
Skip to first unread message

Adam Tauno Williams

unread,
Oct 6, 2009, 6:40:26 AM10/6/09
to sqlalchemy
I'm using SQLAlchemy 0.5.4p2 with Python 2.6.0. So far all of my model
has been in the declarative style -

class Task(Base):
""" An OpenGroupare Task object """
__tablename__ = 'job'
object_id = Column("job_id",
Sequence('key_generator'),
primary_key=True)
version = Column("object_version", Integer)
parent_id = Column("parent_job_id", Integer,
ForeignKey('job.job_id'),
nullable=True)
....

But I have one 1:1 relation in my database that would be much easier to
model as just one object.

job_history job_history_info
--------------- ----------------------------
job_history_id (PK) <-1:1-> job_history_id
object_version job_history_info_id (PK)
job_id comment
actor_id db_status
action
action_date
job_status
db_status

I've found at least one essay about doing this using the "traditional"
style of mapping @
<http://parijatmishra.wordpress.com/2009/01/18/sqlalchemy-one-classes-two-tables/> Is this possible using the declarative style? If so are there any examples someone can point me to?

Is there, in general, a way to specify that a join is 1:1 so that the
mapper property returns the entity on the other side of the join rather
than a single element array?

Michael Bayer

unread,
Oct 6, 2009, 9:56:37 AM10/6/09
to sqlal...@googlegroups.com
Adam Tauno Williams wrote:
> But I have one 1:1 relation in my database that would be much easier to
> model as just one object.
>
> job_history job_history_info
> --------------- ----------------------------
> job_history_id (PK) <-1:1-> job_history_id
> object_version job_history_info_id (PK)
> job_id comment
> actor_id db_status
> action
> action_date
> job_status
> db_status
>


if you create Table objects for job_history and job_history_info, you can
create a join via job_history.join(job_history_info), and then specify
that to a declarative class using "__table__ = <myjoin>" instead of
__tablename__. you will also want to equate job_history_id in both
tables to a single attribute, as in
http://www.sqlalchemy.org/docs/05/mappers.html#mapping-a-class-against-multiple-tables
, which is accomplished with declarative in a similar way, i..e. "id =
[job_history.c.job_history_id, job_history_info.c.job_history_id".

>
> Is there, in general, a way to specify that a join is 1:1 so that the
> mapper property returns the entity on the other side of the join rather
> than a single element array?

a map to a join is always "1:1" from the object perspective, but if there
are multiple job_history_info rows for one job_history row, those would
typically be expressed as different identities within the mapping. the
primary key of your mapping defaults to [job_history.job_history_id,
job_history_info.job_history_info_id].

Adam Tauno Williams

unread,
Jan 26, 2010, 7:36:17 AM1/26/10
to sqlal...@googlegroups.com

Ok, I've spent quite a bit of time trying to get this do work. And I've
completely failed! I've searched the Internet high-and-low and cannot
find a *single* example of such a joined entity using the declarative
syntax, anywhere.

If someone is willing to make a working example of this [I'll write-up a
detailed description] joining these two tables as one entity I'll (a)
send you $50US via paypal, post the example back here, and to my BLOB
(license: MIT/X11).

--
OpenGroupware developer: awil...@whitemice.org
<http://whitemiceconsulting.blogspot.com/>
OpenGroupare & Cyrus IMAPd documenation @
<http://docs.opengroupware.org/Members/whitemice/wmogag/file_view>

signature.asc

Adam Tauno Williams

unread,
Jan 26, 2010, 9:26:58 AM1/26/10
to sqlal...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages