[Tutor] Object creation query

2019-08-09 Thread mhysnm1964
  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/')

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? 

 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Object creation query

2019-08-09 Thread Peter Otten
mhysnm1...@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/')
> 
> 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, 30.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Object creation query

2019-08-09 Thread Alan Gauld via Tutor
On 09/08/2019 09:54, mhysnm1...@gmail.com wrote:

> updates and insertions. I have multiple tables with the same structure with
> different names.

Umm, why?
Assuming by structure you mean they have the same fields
then that's usually a bad design decision.
Why not have one table with an attribute "name" or "Type"
or similar?

That should greatly simplify your code.

If that's not what you mean by the same structure you need
to give us a little more detail. Not necessarily the full
descriptions but some simplified examples maybe?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Object creation query

2019-08-09 Thread bob gailer

On 8/9/2019 7:39 AM, Alan Gauld via Tutor wrote:

On 09/08/2019 09:54, mhysnm1...@gmail.com wrote:


updates and insertions. I have multiple tables with the same structure with
differe

I agree 100% with Peter and Alan's responses.

--
Bob Gailer

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Object creation query

2019-08-10 Thread Alan Gauld via Tutor
On 10/08/2019 09:48, mhysnm1...@gmail.com wrote:
> HI all,
>
> I have multiple different accounts that I am creating reports for. Thus I
> cannot have a centralised transaction table that contains all the records.
> These different accounts are for different organisations or for other
> members in my family.

That shouldn't make any difference. It would be impractical to have
different
tables for each customer or organisation in something like a large utility
company or say., Amazon where there will be literally millions of account
holders. You would need millions of tables which would be a nightmare to
manage.

The standard model for these kinds of scenarios is a single account table
with a reference to the customer(or user) table and a transaction table
with
a reference to the account. That supports the concept of multiple
customers,
each with (potentially)multiple accounts and each account having multiple
transactions.

> I hope this explains what I am trying to do. I am trying to make
things as
> dynamic as possible without myself recoding the model.

You can make it as dynamic as you like using a single table and you
certainly
shouldn't need to recode the model, although I'm not familiar with
SQLAlchemy
(which is the ORM wrapper you are using I think?) But you might need to
tweak you object definition to make it single table based.

> Also was wondering if it was possible as well.

Anything is possible, it just takes more effort. In this case quite a
lot more effort.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor