def reference_col(tablename, nullable=False, pk_name='id', **kwargs): """Column that adds primary key foreign key reference. Usage: :: category_id = reference_col('category') category = relationship('Category', backref='categories') """ return db.Column(ForeignKey('{0}.{1}'.format(tablename, pk_name)), nullable=nullable, **kwargs)
class SurrogatePK(object): """A mixin that adds a surrogate integer 'primary key' column named ``id`` to any declarative-mapped class.""" __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) @classmethod def get_by_id(cls, record_id): """Get record by ID.""" if any((isinstance(record_id, basestring) and record_id.isdigit(), isinstance(record_id, (int, float))), ): return cls.query.get(int(record_id)) return None