在开发Flask web应用的时候,习惯了用mysql workbench 设计数据库表,直接写Python的SQLAlchemy 还是觉得慢。自然会想到有没有工具可以直接从数据库生成Python的SQLAlchemy呢, sqlacodegen 正好满足需求。
使用步骤。
1. 安装sqlacodegen
pip install sqlacodegen
2. 生成SQLAlchemy 命令
sqlacodegen mysql://root:password@127.0.0.1/crm
# coding: utf-8
from sqlalchemy import Column, Float, Integer, SmallInteger,
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class Attribute(Base):
__tablename__ = 'attribute'
id = Column(Integer, primary_key=True)
attribute = Column(String(128, u'utf8_unicode_ci'))
att_value = Column(String(255, u'utf8_unicode_ci'))
serivce_id = Column(Integer)
description = Column(String(255, u'utf8_unicode_ci'))