[Tutor] Object creation query
Peter Otten
__peter__ at web.de
Fri Aug 9 06:56:42 EDT 2019
mhysnm1964 at gmail.com wrote:
> All,
>
>
>
> I think I am asking for the impossible here. But I will ask anyway.
>
>
>
> I am using flask_sqlalchemy to build the tables and perform the queries,
> updates and insertions. I have multiple tables with the same structure
> with different names. A table called accounts which stores the name of the
> tables with the same structures. This is the important bits to know about.
>
>
>
> I have a page called transactions. When I call this page, I can append
> different names to the end. For example:
>
>
>
> Transactions/account1
>
> Transactions/account2
>
> Transactions/account3
>
> .
>
>
>
> In the view for transactions I am doing the following (code extract)
>
>
>
> @app.route('/transactions/<account>')
>
> def transactions(account):
>
> if accounts != "Transactions":
>
> Accounts.query.filter_by(account_name =account).first_or_404()
>
> tables = Accounts.query.all()
>
> if account == 'Account1':
>
> records = Account1
>
> elif account == 'Account2':
>
> records = Account2
>
> records = records.query.order_by(records.date.desc)
>
>
>
> as I am saving each model object into the same variable depending on the
> full URL name. I am wondering if there is a better way in doing this
> rather than using a list of if tests?
How about using only one table AccountEntries with an AccountId column
which would also be the primary key of the Accounts table. Then
# pseudo code
# look up account
wanted_accountId = select acountId
from Accounts
where name = account
# find records for account
records = select * from AccountEntries
where accountId = wanted_accountId
order by date desc
That design would greatly simplify adding accounts 3 to, say, 300000.
More information about the Tutor
mailing list