> > > you'd need to implement a generate_relationship function as described at > http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#custom-relationship-arguments > > which applies the rules you want in order to establish those relationships > that you'd like to be one-to-one. > > Yeah, that's what I tried to do here, but it appeared to do nothing. That documentation isn't entirely clear I'm afraid. The direction input doesn't have an interaction.ONETOONE option, so I tried to find an alternative way of identifying which tables needed a one-to-one relationship. Having a look, I don't think this function works as is because I don't define the pair of tables (from local_cls, referred_cls) that are in a one-to-one. I only define one in my onetoones list.
onetoones = ['file'] 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) > > > Here is what I'm trying so far, but it's not working. >> > > > I don't see anything obviously wrong with it but you'd want to step > through with pdb.set_trace() to ensure every aspect of it is doing what > you'd expect. Otherwise "not working" can mean lots of things. > > Ok. Well I'll keep digging around. > > >> >> >> > > >> onetoones = ['file'] >> >> 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) >> >> def camelizeClassName(base, tablename, table): >> return str(tablename[0].upper() + re.sub(r'_([a-z])', lambda m: >> m.group(1).upper(), tablename[1:])) >> >> _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) >> >> # Pre-define Dap class. Necessary so automap knows to join this table to >> a declarative base class from another schema >> class Dap(Base): >> __tablename__ = 'dap' >> >> cube_pk = Column(Integer, ForeignKey(datadb.Cube.pk)) >> cube = relationship(datadb.Cube, backref='dap', uselist=False) >> >> # Prepare the base >> 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.