开发者

python class attributes and sql alchemy relationships

Ok so I have two problems:

My first would by sqlalchemy related. So I'm forced to use sqllite and I have to implement some cascade deletes. Now I've found relationships do the job however these should be declared normally in the parent table. But due to some design decision I'm forced to do this in the child so I'm doing something like:

class Operation(Base):
    """
    The class used to log any action executed in Projects.
    """
    __tablename__ = 'OPERATIONS'

    id = Column(Integer, primary_key=True)
    parameters = Column(String)
    ..... rest of class here ....

class DataType(Base):

    __tablename__ = 'DATA_TYPES'
    id = Column(Integer, primary_key=True)
    gid = Column(String)
    ...more params...
    parent_operation = relationship(Operation, backref=backref("DATA_TYPES", 
                                                    order_by=id, 
                                                    cascade="all,delete"))
    ...rest of class...

Now this seems to work but I'm still not certain of a few things.

  • Firstly, what can I do with parent_operation from here on end? I mean I see that the cascade works but I make no use of parent_operation except for the actual declaration.

    • Secondly, the "DATA_TYPES" in the above case, which is the first parameter in the backref, does this need to be the name of the child table or does it need to be unique per model?

    • And finally, in my case both Operation and DataType classes are in the same module, so I can pass Operation as the first parameter in the relationship. Now if this wasnt the case and I would have them in separate modules, if I still want to declare this relationship should I pass 'Operation' or 'OPERATION' to the relationship( Classname or Tablename ? )


Now my second is more core Python but since it still has some connections with the above I'll add it here. So I need to be able to add a class attribute dinamically. Basically I need to add a relationship like the ones declared above.

class BaseClass(object)    

def __init__(self):
    my_class = self.__class__
    if not hasattr(my_class, self.__class__.__name__):
        reference = "my_class." + self.__class__.__name__ + "= relationship\
        ('DataType', backref=backref('" + sel开发者_如何转开发f.__class__.__name__ + "', \
        cascade='all,delete'))"
        exec reference

The reason of to WHY I need to do this are complicated and have to do with some design decisions(basically I need every class that extends this one to have a relationship declared to the 'DataType' class). Now I know using of the exec statement isn't such a good practice. So is there a better way to do the above?

Regards, Bogdan


For the second part or your question, keep in mind, anything in your class constructor won't be instrumented by SqlAlchemy. In your example you can simply declare the relationship in its own class (note it does not inherit from you declarative_base class) and then inherit it in any subclass something like this:

class BaseDataType(object):
    parent_operation = relationship(Operation, backref="datatype")

class DataTypeA(Base, BaseDataType):
    id = Column(Integer, primary_key=True)

class DataTypeB(Base, BaseDataType):
    id = Column(Integer, primary_key=True)

The SqlAlchemy documentation gives good examples of what's possible:

http://www.sqlalchemy.org/docs/orm/extensions/declarative.html#mixing-in-relationships

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜