Question

I am looking for a web framework (hopefully Python, Postgresql and ORM based) that can handle the following scenario:

You have a Person table (with one person Avril Lavigne - who had two marriages):

PersonID Start       End         PersonName
-------- ----------- ----------- -------------
1        27-Sep-1984 14-Jul-2006 Avril Lavigne
1        15-Jul-2006 30-Jun-2013 Avril Whibley
1        01-Jul-2013 31-Dec-9999 Avril Kroeger

You have an Assignment Table (which is the job assignment etc. that a person holds, where a person can hold more than one in various locations):

AssignID Start       End         PersonID JobID LocID
-------- ----------- ----------- -------- ----- -----
1        27-Sep-1984 26-Sep-1999 1        1     1 
1        27-Sep-1999 31-Dec-2002 1        1     2
1        01-Jan-2003 31-Dec-9999 1        1     3
2        01-Jul-2013 31-Dec-9999 1        2     3

Is there a way in which Pyramid can have a user select an effective date (say today) and SQL Alchemy will be able to join the tables using the effective date and a SQL between, and a foreign key join on PersonID; to bring back:

Avril Kroeger has 2 assignments with JobID 1 and 2 and LocID 3.

Where the SQL statement in Oracle would have been:

select p.PersonName, 
       a.JobID,
       a.LocID
from   Person p,
       Assignment a
where  a.PersonID = p.PersonID
and    trunc(sysdate) between a.Start and a.End
and    trunc(sysdate) between p.Start and p.End

I'm just worried I'm going to learn another web framework which cannot handle composite primary keys and the above scenario which even SQL foreign keys can't handle.

So basically can the ORM handle arbitrary queries? Do you loose all the ORM shorthand functions if you go for the above scenario? Is there a better way to handle the above scenario. Should I store the effective date as a session variable?

I don't know enough about the topic to provide my solution. I'm more trying to avoid learning yet another framework which can't solve the above scenario.

Was it helpful?

Solution

Firstly, Pyramid can absolute totally handle this case simply because Pyramid is storage-agnostic, so it does not know anything about databases, SQL and other such stuff :) In other words, the question is not related to Pyramid in any way.

Secondly, SQLAlchemy does support composite primary keys. From the top of my head, the declarative classes would look like this:

class Person(sa.Base):
    person_id = sa.Column(sa.Integer, primary_key=True)
    start = sa.Column(sa.DateTime, primary_key=True)
    end = sa.Column(sa.DateTime, primary_key=True)
    name = sa.Column(sa.String)

class Assignment(sa.Base):
    person_id = sa.Column(sa.Integer, primary_key=True)
    start = sa.Column(sa.DateTime, primary_key=True)
    end = sa.Column(sa.DateTime, primary_key=True)
    ...

and your query would look like this:

(session.query(Person.name, Assignment.jobId, Assignment.LocID)
    .filter(Assignment.person_id==Person.person_id)
    .filter(sa.sql.functions.now().between(Person.start, Person.end))
    .filter(sa.sql.functions.now().between(Assignment.start, Assignment.end))
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top