On Sat, May 15, 2010 at 02:44, clane <[email protected]> wrote:
> Hi all!
>
> I'm building a database of football stats. I have the following setup
> right now:
>
> class Game(Entity):
>    using_options(tablename="games")
>    rushing = OneToOne("Rushing", inverse="game")
>    passing = OneToOne("Passing", inverse="game")
>
> class Rushing(Entity):
>    using_options(tablename="rushing")
>    attempts = Field(Float)
>    yards = Field(Float)
>    game = ManyToOne("Game")
>    yards_per_attempt = ColumnProperty(lambda c: c.yards/c.attempts)
>
> class Passing(Entity):
>    using_options(tablename="passing")
>    attempts = Field(Float)
>    yards = Field(Float)
>    game = ManyToOne("Game")
>    yards_per_attempt = ColumnProperty(lambda c: c.yards/c.attempts)
>
> So, I'd like to add a ColumnProperty to Game that summed up rushing
> and passing yards, something like,
>
>    total_yards = ColumnProperty(lambda c: c.rushing.yards +
> c.passing.yards)
>
> Obviously, the above code doesn't work, but I'm curious if there's a
> way to do it? Now, I could just write a `total_yards' method on Game
> that sums everything up, but I like the idea of having it
> automatically calculated once the table is defined. I'm new to
> SQLAlchemy and elixir, so I'm hoping I'm just missing something
> obvious :)

You need to embed a "select" expression for stuff like that to work.

For an example, see:
http://elixir.ematia.de/trac/browser/elixir/tags/0.7.1/tests/test_properties.py#L64

-- 
Gaëtan de Menten

-- 
You received this message because you are subscribed to the Google Groups 
"SQLElixir" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlelixir?hl=en.

Reply via email to