Mihai H
Mihai H

Reputation: 3291

SQLAlchemy - Classical map relations

I am struggling a bit with SQLAlchemy classical mapping. My scenario is the following one:

class Manager(object):
    def __init__(self):
       self.id = uuid.uuid4()
       .....

class Client(object):
    def __init__(self):
        self.id = uuid.uuid4()
        self.manager = None

What I want to do is to convince SQLAlchemy to create a relationship between Client and Manager (where a client has a manager) whiteout having to define a manager id attribute at client level. In the code above 'manager' attribute would be an instance of Manager class. I am using classical mapping in this case. Any help would be much appreciated.

mapper(Client, self._table, column_prefix='_', properties={'managed_by_relation':relationship(User, order_by=self._table.c.id, foreign_keys=[self._table.c.manager_id]))

The above code describes what I am trying to avoid.

Upvotes: 2

Views: 420

Answers (1)

zzzeek
zzzeek

Reputation: 75117

seems like maybe you're looking for some way to automate creation of the ForeignKey? I wrote a blog post about one possible approach to simulating the more "traditional" declarative ORM here: magic, a "new" orm

Upvotes: 1

Related Questions