What's important here is how you would use these classes. that is, if you want 
to have them all laid out explicitly, and your code will do things like " 
session.query(Table157).all() ", that is, refer to them explicitly, versus 
these tables are all part of some kind of dynamic data structure, like 
"session.query(tables[table_id])" or something like that.

the general pattern for making lots of classes of the same structure, when you 
are also using the ORM, is to either use mixins, or to just create the classes 
dynamically. 

There's a wiki recipe called "EntityName" that shows this, and I've just 
updated / reformatted it to be more or less current:

https://github.com/sqlalchemy/sqlalchemy/wiki/EntityName

note the second example that shows declarative with a mixin, and shows how you 
can use the type() function to generate new classes dynamically. Creating new 
classes in Python is quite open ended.


On Fri, Jun 5, 2020, at 10:21 PM, Richard Damon wrote:
> I am working on a project where I am using (and learning) SQLAlchemy (to
> an SQLite database if that matters) where I am seeing the need for a
> number of very similar tables, and would like to follow the DRY (Don't
> Repeat Yourself) principle if possible.
> 
> Each of these tables will have 3 fields (actually, a few more, but these
> are the key ones that are of interest)
> 
> An Integer Field that is a Foreign Key to another Table (and which table
> it is varies on each of the tables being generated)
> 
> A Second Integer Field that is also a Foreign Key, but for all the
> tables this is to the same table (it is in fact specifying a language)
> 
> A Third field, which will be a text field. (The name of the item
> specified by the First Field, in the language specified by the second)
> 
> 
> The first two fields together will be the Primary Key for the table. The
> second and third together will have a unique constraint on them.
> 
> I am somewhat new to Python (but an experienced programmer and tend to
> learn fast), and trying to figure out the best way to do this while
> trying to minimize repeated code.
> 
> One thought that comes to mind is a base class, but then I have only 1
> class directly derived from declarative_base, but that class itself
> shouldn't generate a table, doesn't have all the information itself to
> make a table, and I am not sure how to pass down to it the information
> about the variable foreign key.
> 
> A second thought that looks promising would be a class decorator for the
> class (looks like it should work, but a bit of work to learn how to
> build that).
> 
> Something else I didn't think of ?
> 
> There are going to be a collection of routines to do some of the
> generalized work for these tables, taking some sort of description of
> which table to work with, which could be put in the base class, or the
> decorator could just duck type the needed hooks into the class.
> 
> -- 
> Richard Damon
> 
> -- 
> SQLAlchemy - 
> The Python SQL Toolkit and Object Relational Mapper
> 
> http://www.sqlalchemy.org/
> 
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> --- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/5771fcb2-6730-e2b8-6555-d27a39db6505%40Damon-Family.org.
> 

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/18d4a757-5dcb-49b0-91fe-35d5b73c7598%40www.fastmail.com.

Reply via email to