thanks again Mike! i'll work this here :)

my best regards,
richard.


On 06/10/2014 07:14 PM, Mike Bayer wrote:
On Tue Jun 10 15:36:09 2014, Richard Gerd Kuesters wrote:
so, here i am again with another weird question, but it may be
interesting for what it may come (i dunno yet).

the problem: i have a collection of abstract classes that, when
"requested", the function (that does the request) checks in a internal
dictionary if that class was already created or creates it by using
"declarative_base(cls=MyAbstractClass)", that later can have an engine
and then work against a database.

i use this format because i work with multiple backends from multiple
sources, so abstract classes are a *must* here. now, the problem:
foreign keys and relationships. it's driving me nuts.

ok, let's say I have 2 classes, Foo and Bar, where Bar have one FK to Foo.


    class Foo(object):
        __abstract__ = True
        foo_id = Column(...)
        ...

    class Bar(object):
        __abstract__ = True
        foo_id = Column(ForeignKey(...))
        ....



/(those classes are just examples and weren't further coded because
it's a conceptual question)/

i know that the code might be wrong, because i can use @declared_attr
here and furthermore help sqlalchemy act accordingly (i don't know if
this is the right way to say it in english, but it is not a complain
about sqlalchemy actions).

ok, suppose I created two subclasses, one from each abstract model
(Foo and Bar) in a postgres database with some named schema, let's say
"sc1". we then have "sc1.foo" and "sc1.bar".

now, i want to create a third table, also from Bar, but in the "sc2"
schema, where its foreign key will reference "sc1.foo", which postgres
supports nicely.

how can i work this out, in a pythonic and sqlalchemy friendly way?
does @declared_attr solves this? or do I have to map that foreign key
(and furthermore relationships) in the class mapper, before using it,
like a @classmethod of some kind?


@declared_attr can help since the decorated function is called with "cls" as an argument. You can look on cls for "__table_args__" or some other attribute if you need, and you can create a Table on the fly to serve as "secondary", see http://docs.sqlalchemy.org/en/rel_0_9/_modules/examples/generic_associations/table_per_related.html for an example of what this looks like.





best regards and sorry for my english,
richard.

--
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
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


--
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to