build a sqlalchemy model for given prop
(
cls: declarative.DeclarativeMeta,
prop: T_PROPS,
schema: str = None)
| 246 | |
| 247 | |
| 248 | def build_history_class( |
| 249 | cls: declarative.DeclarativeMeta, |
| 250 | prop: T_PROPS, |
| 251 | schema: str = None) -> nine.Type[TemporalProperty]: |
| 252 | """build a sqlalchemy model for given prop""" |
| 253 | class_name = "%s%s_%s" % (cls.__name__, 'History', prop.key) |
| 254 | table = build_history_table(cls, prop, schema) |
| 255 | base_classes = ( |
| 256 | TemporalProperty, |
| 257 | declarative.declarative_base(metadata=table.metadata), |
| 258 | ) |
| 259 | class_attrs = { |
| 260 | '__table__': table, |
| 261 | 'entity': orm.relationship( |
| 262 | lambda: cls, |
| 263 | backref=orm.backref('%s_history' % prop.key, lazy='dynamic'), |
| 264 | ), |
| 265 | } |
| 266 | |
| 267 | if isinstance(prop, orm.RelationshipProperty): |
| 268 | class_attrs[prop.key] = orm.relationship(prop.argument, lazy='noload') |
| 269 | |
| 270 | model = type(class_name, base_classes, class_attrs) |
| 271 | return model |
| 272 | |
| 273 | |
| 274 | def _generate_history_table_name(local_table: sa.Table, |