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.

Reply via email to