Yeah I realized I have somewhat nuanced relationships for automap to
handle, and that in all the time I spent trying to figure out how to get
automap working for me, I could have written my classes in explicit
declarative base. So that's what I ended up doing. I was hoping to bypass
that a bit since I have a large number of tables to declare. Thanks for
your explanations and help though. I appreciate it.
On Sunday, February 14, 2016 at 5:01:19 PM UTC-5, Brian Cherinka wrote:
>
> What is the proper way to get pluralized shortened names for many-to-many
> tables when using automap? I currently have it set to generate pluralized
> lowercase names for collections instead of the default "_collection". This
> is what I want for one-to-many or many-to-one relationships, but not
> many-to-many. For example, I have two tables, hdu, and extcol, joined
> together through a many-to-many table, hdu_to_extcol
>
> create table hdu (pk serial primary key not null, extname_pk integer,
> exttype_pk integer, extno integer, file_pk integer);
> create table hdu_to_extcol (pk serial primary key not null, hdu_pk integer
> , extcol_pk integer);
> create table extcol (pk serial primary key not null, name text);
>
> ALTER TABLE ONLY mangadapdb.hdu_to_extcol
> ADD CONSTRAINT hdu_fk
> FOREIGN KEY (hdu_pk) REFERENCES mangadapdb.hdu(pk)
> ON UPDATE CASCADE ON DELETE CASCADE;
>
> ALTER TABLE ONLY mangadapdb.hdu_to_extcol
> ADD CONSTRAINT extcol_fk
> FOREIGN KEY (extcol_pk) REFERENCES mangadapdb.extcol(pk)
> ON UPDATE CASCADE ON DELETE CASCADE;
>
> When I use SQLalchemy to automap the Base classes, the relationship this
> generates on the Hdu and Extcol classes are *Hdu.hdu_to_extcol, and
> Extcol.hdu_to_extcols*, using the below pluralize, and relationship,
> code. However, ideally what I'd like the names to be are *Hdu.extcols,
> and Extcol.hdus*, respectively. What's the best to generate this for
> these many-to-many tables? I'm not sure if automap is recognizing these
> as many-to-many tables. The direction indicated when I print during the
> relationship stage don't indicate as such.
>
> symbol('ONETOMANY') <class 'sqlalchemy.ext.automap.Extcol'> extcol
> symbol('MANYTOONE') <class 'sqlalchemy.ext.automap.HduToExtcol'>
> hdu_to_extcol
> symbol('ONETOMANY') <class 'sqlalchemy.ext.automap.Hdu'> hdu
> symbol('MANYTOONE') <class 'sqlalchemy.ext.automap.HduToExtcol'>
> hdu_to_extcol
>
> Here is my Base class generation code.
>
> def _gen_relationship(base, direction, return_fn, attrname, local_cls,
> referred_cls, **kw):
> if local_cls.__table__.name in onetoones:
> kw['uselist'] = False
> # make use of the built-in function to actually return the result.
>
> return generate_relationship(base, direction, return_fn, attrname,
> local_cls, referred_cls, **kw)
>
> _pluralizer = inflect.engine()
> def pluralize_collection(base, local_cls, referred_cls, constraint):
> referred_name = referred_cls.__name__
> uncamelized = re.sub(r'[A-Z]', lambda m: "_%s" % m.group(0).lower(),
> referred_name)[1:]
> pluralized = _pluralizer.plural(uncamelized)
> return pluralized
>
> # Grabs engine
> db = DatabaseConnection()
> engine = db.engine
>
> # Selects schema and automaps it.
> metadata = MetaData(schema='mangadapdb')
> Base = automap_base(bind=engine, metadata=metadata)
> Base.prepare(engine, reflect=True, classname_for_table=camelizeClassName,
> name_for_collection_relationship=pluralize_collection,
> generate_relationship=_gen_relationship)
>
> # Explicitly declare classes
> for cl in Base.classes.keys():
> exec('{0} = Base.classes.{0}'.format(cl))
>
>
>
>
>
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.