我开发了一个基于SQLAlchemy的框架。在我的映射器中导入表的代码如下:
for table in tables:
columns = []
relationships = {}
for column in inspect.getmembers(table,
lambda a: not (inspect.isroutine(a)) and (
type(a) == Column or type(a) == RelationshipProperty)):
if type(column[1]) == Column:
column[1].name = column[0]
columns.append(column[1])
else:
column[1].argument = list(filter(lambda t: t.__name__ == column[1].argument, tables))[0]
relationships[column[0]] = column[1]
sql_table = Table(table.__tablename__,
base.metadata,
*columns,
extend_existing=True)
mapper(table, sql_table, properties=relationships)
base.metadata.create_all()
然后我定义了各种表:Permission.py:
class Permission(TableModel):
__tablename__ = 'permission'
network_id = Column(String(255), ForeignKey("network.id", ondelete="CASCADE"), primary_key=True,
default=check_network_validity)
user_id = Column(Integer, ForeignKey("user.id", ondelete="CASCADE"), primary_key=True)
user = relationship("User", back_populates="permissions",
foreign_keys=user_id)
network = relationship("Network", back_populates="permissions",
foreign_keys=network_id)
type = Column(Enum(TypeEnum), primary_key=True)
validated_by = Column(Integer, ForeignKey("user.id", ondelete="CASCADE"), nullable=True, default=None)
end_validity = Column(DateTime, nullable=True)
created = Column(DateTime, default=datetime.utcnow())
last_updated = Column(DateTime, default=datetime.utcnow(), onupdate=datetime.utcnow())
任何想法?甚至顶测试一些东西?
至尊宝的传说
相关分类