class Base(db.Model): __abstract__ = True id = db.Column('id', db.Integer, primary_key=True, unique=True) created_at = db.Column('created_at', db.DateTime(timezone=True), default=db.func.current_timestamp()) updated_at = db.Column('updated_at', db.DateTime(timezone=True), default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) def create(self): db.session.add(self) db.session.commit() db.session.flush() def update(self): db.session.commit() db.session.flush() def bulk_insert(self, data): db.session.bulk_insert_mappings(self, data) db.session.commit() db.session.flush() def last_updated_id(self, class_name): return db.session.query(class_name).order_by(class_name.id.desc()).first() def save(self): db.session.add(self) db.session.commit() return self def delete(self): db.session.delete(self) db.session.commit() @classmethod def merge(cls, obj): db.session.merge(obj) db.session.commit()
class State(Base): __tablename__ = 'state' state_name = db.Column(db.String(25), unique=True) districts = db.relationship("District")
class Station(Base): __tablename__ = 'station' ac_id = db.Column(db.Integer, db.ForeignKey('ac.id')) station_name = db.Column(db.String(100))
class AC(Base): __tablename__ = 'ac' district_id = db.Column(db.Integer, db.ForeignKey('district.id')) ac_name = db.Column(db.String(50), unique=True) stations = db.relationship("Station")
class District(Base): __tablename__ = 'district' state_id = db.Column(db.Integer, db.ForeignKey('state.id')) district_name = db.Column(db.String(50), unique=True) acs = db.relationship("AC")