sqlalchemy创建数据模型
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, Text, Date, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
from settings import DB_URI
engine = create_engine(DB_URI)
Base = declarative_base(engine)
Session = sessionmaker(bind=engine)
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String(50))
password = Column(String(50))
def __repr__(self):
return "<User(id='%s',username='%s',password='%s')>" % (self.id, self.username, self.password)
class Admin(Base):
__tablename__ = 'admin'
id = Column(Integer, primary_key=True)
username = Column(String(50))
password = Column(String(50))
def __repr__(self):
return "<Admin(id='%s',username='%s',password='%s')>" % (self.id, self.username, self.password)
class DataBar(Base):
__tablename__ = 'databar'
id = Column(Integer, primary_key=True)
label = Column(Integer)
data = Column(Integer)
def __repr__(self):
return "<DataBar(id='%s',label='%s',data='%s')>" % (self.id, self.label, self.data)
class DataLine(Base):
__tablename__ = 'dataline'
id = Column(Integer, primary_key=True)
label = Column(Integer)
data = Column(Integer)
def __repr__(self):
return "<DataLine(id='%s',label='%s',data='%s')>" % (self.id, self.label, self.data)
class Unit(Base):
__tablename__ = 'unit'
id = Column(Integer, primary_key=True)
unitname = Column(String(50))
contents = relationship('Content', back_populates='unit')
def __repr__(self):
return "<Unit(id='%s',unitname='%s')>" % (self.id, self.unitname)
class Category(Base):
__tablename__ = 'category'
id = Column(Integer, primary_key=True)
category = Column(String(50))
contents = relationship('Content', back_populates='category')
def __repr__(self):
return "<Unit(id='%s',unitname='%s')>" % (self.id, self.unitname)
class Content(Base):
__tablename__ = 'content'
id = Column(Integer, primary_key=True)
problem = Column(Text)
cid = Column(Integer, ForeignKey('category.id'))
uid = Column(Integer, ForeignKey('unit.id'))
date = Column(Date)
category = relationship('Category', back_populates='contents')
unit = relationship('Unit', back_populates='contents')
Flask-SQLAlchemy创建数据模型
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Admin(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), unique=True)
password = db.Column(db.String(50))
def __init__(self, username, password):
self.username = username
self.password = password
def __repr__(self):
return "<Admin(id='%s',username='%s',password='%s')>" % (self.id, self.username, self.password)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), unique=True)
password = db.Column(db.String(50))
def __init__(self, username, password):
self.username = username
self.password = password
def __repr__(self):
return "<User(id='%s',username='%s',password='%s')>" % (self.id, self.username, self.password)
class DataBar(db.Model):
id = db.Column(db.Integer, primary_key=True)
label = db.Column(db.Integer)
data = db.Column(db.Integer)
def __init__(self, label, data):
self.label = label
self.data = data
def __repr__(self):
return "<DataBar(id='%s',label='%s',data='%s')>" % (self.id, self.label, self.data)
class DataLine(db.Model):
id = db.Column(db.Integer, primary_key=True)
label = db.Column(db.Integer)
data = db.Column(db.Integer)
def __init__(self, label, data):
self.label = label
self.data = data
def __repr__(self):
return "<DataLine(id='%s',label='%s',data='%s')>" % (self.id, self.label, self.data)
class Unit(db.Model):
id = db.Column(db.Integer, primary_key=True)
unitname = db.Column(db.String(50))
contents = db.relationship('Content', back_populates='unit')
def __init__(self, unitname):
self.unitname = unitname
def __repr__(self):
return "<Unit(id='%s',unitname='%s')>" % (self.id, self.unitname)
class Category1(db.Model):
id = db.Column(db.Integer, primary_key=True)
category = db.Column(db.String(50))
contents = db.relationship('Content', back_populates='category1')
category2s = db.relationship('Category2', back_populates='category1')
def __init__(self, category):
self.category = category
def __repr__(self):
return "<Unit(id='%s',category='%s')>" % (self.id, self.category)
class Category2(db.Model):
id = db.Column(db.Integer, primary_key=True)
category = db.Column(db.String(50))
c1id = db.Column(db.Integer, db.ForeignKey('category1.id'))
contents = db.relationship('Content', back_populates='category2')
category1 = db.relationship('Category1', back_populates='category2s')
def __init__(self, category, c1id):
self.category = category
self.c1id = c1id
def __repr__(self):
return "<Unit(id='%s',category='%s',c1id='%s)>" % (self.id, self.category, self.c1id)
class Content(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
problem = db.Column(db.Text)
c1id = db.Column(db.Integer, db.ForeignKey('category1.id'))
c2id = db.Column(db.Integer, db.ForeignKey('category2.id'))
uid = db.Column(db.Integer, db.ForeignKey('unit.id'))
date = db.Column(db.Date)
modificationstate = db.Column(db.Boolean)
modificationdate = db.Column(db.Date)
category1 = db.relationship('Category1', back_populates='contents')
category2 = db.relationship('Category2', back_populates='contents')
unit = db.relationship('Unit', back_populates='contents')
def __init__(self, problem, c1id, c2id, uid, date, modificationstate, modificationdate):
self.problem = problem
self.c1id = c1id
self.c2id = c2id
self.uid = uid
self.date = date
self.modificationstate = modificationstate
self.modificationdate = modificationdate
def __repr__(self):
return "<Content(id='%s',problem='%s',c1id='%s',c2id='%s',uid='%s',date='%s'),modificationstate='%s',modificationdate='%s'>" \
% (self.id, self.problem, self.c1id, self.c2id, self.uid, self.date, self.modificationstate, self.modificationdate)