On Wednesday, 25 July 2012 16:44:16 UTC+1, Michael Bayer wrote:

just build a function:
>
> def create_my_class(x, y, z, ...):
>     class MyClass(Base):
>         __tablename__ = '...'
>         # ...
>
>    MyClass.__name__ = 'SomeName%s%s' % (q, p)
>    return MyClass
>
>
> On Jul 25, 2012, at 5:49 AM, Stephan Hügel wrote:
>
> I need to create 20 identical (in structure) tables, each of which will 
> have a many-to-many relationship with a particular table (Table_A).
>
> I've thought a bit about this, and there doesn't seem to be a better way 
> to structure the setup; it's a canonical reference (Table_A), each entry of 
> which can have multiple overlapping entries in a particular book (each of 
> the 20 tables represents references in a particular book).
> Is there a sensible, compact way for me to instantiate the 20 classes and 
> association tables? Their structure is extremely simple; just a primary key 
> column and a string column.
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/sqlalchemy/-/fVKaMEuyN_IJ.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
>
>
>
 OK, I've done the following

def create_signlist(sl):
    class SignList(db.Model):

        __tablename__ = listname.lower()
 
        id = db.Column("id", db.Integer(), primary_key=True)
        reference = db.Column(db.String(50), nullable=False, unique=True)

        def __init__(self, reference):
            self.reference = reference

    SignList.__name__ = listname
    return SignList

signlists = ['lka', 'kal']
for s in signlists:
    create_signlists(s)

But this gives me a warning:

SAWarning: The classname 'SignList' is already in the registry of this 
declarative base, mapped to <class 'glyph.models.lka'>
  _as_declarative(cls, classname, cls.__dict__)

And e.g. lka.query.all() fails, presumably because there are no instances 
of it yet.

Sorry, I'm missing something completely obvious.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/0BbJ0Q0wNX0J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to