mwan February 2016

Subscription modelling in Flask SQLAlchemy

I am trying to model the following scenario in Flask SQLAlchemy:

There are a list of SubscriptionPacks available for purchase. When a particular User buys a SubscriptionPack they start an instance of that Subscription.

The model is as follows:

ERD

A User can have many Subscriptions (only one of which will be Active at a time) and each Subscription will be referencing one SubscriptionPack.

How would this be modelled in SQLAlchemy?

Currently I have the User.id and SubscriptionPack.id referenced as db.ForeignKey in the Subscriptions model. And I have Subscriptions referenced as a db.Relationship in the Users table. This seems inconsistent and wrong and is leading me to have to hand-code a lot of SQL statements to return the right results.

Any help as to how to do this right?

Answers


mwan February 2016

For those who stumble upon this, what I was looking for was the bidirectional SQLAlchemy Association Object pattern.

This allows the intermediate table of a Many-to-Many to have it's own stored details. In my instance above the Subscription table needed to be an Association Object (has it's own class).

Post Status

Asked in February 2016
Viewed 2,580 times
Voted 6
Answered 1 times

Search




Leave an answer