SqlAlchemy Classes声明两个依赖类

时间:2016-01-08 13:51:19

标签: python sql sqlalchemy

我在文件中遇到问题,我声明了所有类的映射器。

    class Application(AbstractId):

        .........

        key_event_id = ORM.column_property(
                 SA.select([ApplicationEvent.id],
                    correlate = True,
                    from_obj = [Application.__table__.join(ApplicationEvent.__table__)]
                ).as_scalar().label("tag").where(ApplicationEvent.key_event == 1)
            )
            SA.select([ApplicationEvent]).filter(
                        ApplicationEvent.key_event)


    class ApplicationEvent(AbstractId):
        __tablename__ = 'applications_events'

        application_id = SA.Column(SA.Integer, SA.ForeignKey(Application.id), primary_key = True)
        application = ORM.relationship(Application, backref = 'events')
        event_id = SA.Column(SA.Integer, SA.ForeignKey(Event.id), primary_key = True)
        event = ORM.relationship(Event)

这不起作用,因为在Application之前声明了ApplicationEvent。我怎样才能做到这一点?我需要key_event_id作为Application的一列。

这也不起作用:

@declarative.declared_attr
        def key_event_id(cls):  
            return ORM.column_property(
                 SA.select(['ApplicationEvent.id'],
                    correlate = True,
                    from_obj = ['Application.__table__'.join('ApplicationEvent.__table__')]
                ).as_scalar().where('ApplicationEvent.key_event' == 1).label("key_event_id")
            )

2 个答案:

答案 0 :(得分:0)

You can simply pass the model name as a string to the relationship() call.

  

<强>参数

     

表示目标的映射类或实际Mapper实例   关系。

     

参数也可以作为可评估的可调用函数传递   在mapper初始化时,可以作为Python可评估的传递   使用Declarative时的字符串。

答案 1 :(得分:0)

你可以做到

.animate-in{
  left: -20%;
  opacity: 1;
  top: 0;
  z-index: 1;
  -webkit-transform: scale(1.2);
  -moz-transform: scale(1.2);
  -ms-transform: scale(1.2);
  -o-transform: scale(1.2);
  transform: scale(1.2);
  -webkit-transition-property: left, -webkit-transform, opacity;
  -moz-transition-property: left, transform, opacity;
  -ms-transition-property: left, transform, opacity;
  -o-transition-property: left, transform, opacity;
  transition-property: left, transform, opacity;
  -webkit-transition-duration: 10s, 15s, 2s;
  -moz-transition-duration: 10s, 15s, 2s;
  -ms-transition-duration: 10s, 15s, 2s;
  -o-transition-duration: 10s, 15s, 2s;
  transition-duration: 10s, 15s, 2s;
  -webkit-transition-timing-function: linear;
  -moz-transition-timing-function: linear;
  -ms-transition-timing-function: linear;
  -o-transition-timing-function: linear;
  transition-timing-function: linear;
  -webkit-transition-delay: 1s;
  -moz-transition-delay: 1s;
  -ms-transition-delay: 1s;
  -o-transition-delay: 1s;
  transition-delay: 1s;
} 

 application = ORM.relationship("Application", backref = 'events')

你可以这样写

 event = ORM.relationship("Event" , order_by="Event.id")