Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I use dynamic relationships (or VIEWS) in SQLAlchemy ORM?
    primarykey
    data
    text
    <p>I'm looking for a way, so that a relationship <em>only uses some rows of a table</em>, instead of the whole table. </p> <blockquote> <p>I was thinking about using a <strong>view</strong> instead of the original table as base for the relationship. Where the <em>view</em> contains only a filtered set of rows from the original table.</p> </blockquote> <h2>Example</h2> <p>I want to collect statistical information for songs (rating, play-count, last time played) from a couple of players over different devices and from different users. I use <em>Python</em> and <a href="http://www.sqlalchemy.org/" rel="nofollow noreferrer">SQL Alchemy</a>. </p> <p>I came up with the following table layout (you can take a look at the <a href="https://gist.github.com/brutus/7483771" rel="nofollow noreferrer">code for the objects</a> and the <a href="https://gist.github.com/brutus/7335121" rel="nofollow noreferrer">SQL for the tables</a>):</p> <p><img src="https://i.stack.imgur.com/Bbfh4.png" alt="ERP"></p> <h2>The problem</h2> <p>For each <code>Song</code> object I can easily get <em>all the associated</em> <code>Stats</code> objects. But most of the time I only want <em>some of them</em>. I only want those <code>Stats</code> for a <code>Song</code> that relate to one or more <code>Commit</code>, eg. only the data from all commits by a given user, or one certain commit. </p> <p><strong>So I need some way to filter the stats data on the song object.</strong></p> <p>I'm not quiet sure how to archive this. </p> <p>Should I use some custom query? But where do I place it and how? Also: while this might give me the data I want (<code>select path, rating... over the three joined tables</code> or <a href="https://gist.github.com/brutus/7486362" rel="nofollow noreferrer">something along those lines</a>), I won't get objects back.</p> <p>I was thinking about using a <strong>view</strong> on the <code>stats</code> table, containing only the lines matching the given <code>commits</code>. The view has to be created dynamically trough, so different commits can be filtered. And than using this view as the base for a relationship from <code>songs</code> to <code>stats</code>. But I have no idea how to do it.</p> <p>So: Any ideas on how to solve this problem? </p> <p>Or how to solve this another way?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload