class Orderline(db.Model): __tablename__ = 'orderlines' orderlineid = db.Column('orderlineid', db.Integer, primary_key=True) orderid = db.Column('orderid', db.ForeignKey('orders.orderid', ondelete='CASCADE'), primary_key=True) prod_id = db.Column('prod_id', db.ForeignKey('products.prod_id'), nullable=False) quantity = db.Column('quantity', db.SmallInteger, nullable=False) orderdate = db.Column('orderdate', db.Date, nullable=False) order = db.relationship('Order', primaryjoin="Orderline.orderid == Order.id", backref="positions") db.Index('ix_orderlines_orderid', 'orderid', 'orderlineid') def _asdict(self): return { "orderlineid": self.orderlineid, "orderid": self.orderid, "product": self.product, "quantity": self.quantity, "orderdate": self.orderdate }
def make_association_table(fk1, fk2, index=False): """Create an association table basing on names of two given foreign keys. From keys: `site.id` and `kinase.id` a table named: site_kinase_association will be created and it will contain two columns: `site_id` and `kinase_id`. The foreign keys can be provided as models properties, e.g. Model.id, or as SQL strings, e.g. 'model.id'. """ if not isinstance(fk1, str): fk1 = f'{fk1.table}.{fk1.name}' if not isinstance(fk2, str): fk2 = f'{fk2.table}.{fk2.name}' fk1_table = fk1.split('.')[0] fk2_table = fk2.split('.')[0] table_name = f'{fk1_table}_{fk2_table}_association' col1 = fk1.replace('.', '_') col2 = fk2.replace('.', '_') table_args = [ table_name, db.metadata, db.Column(col1, db.Integer, db.ForeignKey(fk1, ondelete='cascade')), db.Column(col2, db.Integer, db.ForeignKey(fk2, ondelete='cascade')), ] if index: table_args.append(db.Index(f'idx_{col1}_{col2}', col1, col2)) return db.Table(*table_args, info={'bind_key': bind})
class todolist(db.Model): __tablename__ = 'todolist' id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=True) username = db.Column(db.String(100), nullable=False) todoitem = db.Column(db.String(150), nullable=False) done = db.Column(db.Boolean, nullable=False) __table_args__ = (db.Index('UI', username, todoitem, unique=True), { 'extend_existing': True}) def __init__(self, username, todoitem, done): self.username = username self.todoitem = todoitem self.done = done
class GeoCoords(Thing, db.Model): __table_args__ = (db.Index('uix_1', 'latitude', 'longitude', unique=True), ) latitude = db.Column( 'latitude', db.Numeric(precision='23,20') ) #String(32)) #Float(precision='23,20') ) # 41.8645403460001 longitude = db.Column('longitude', db.Numeric( precision='23,20')) #String(32)) #Float(precision='23,20') ) elevation = db.Column('elevation', db.Float) addressCountry = db.Column('addressCountry', db.String(1024)) postalCode = db.Column('postalCode', db.String(24)) accuracy = db.Column('accuracy', db.Integer) # Accuracy radius in meters
class StatusCategoryStatusMapping(db.Model): __tablename__ = "status_category_status_mappings" id = db.Column(db.Integer, primary_key=True, nullable=False) activity_id = db.Column(db.Integer, db.ForeignKey("activities.activity_id"), nullable=True) status = db.Column(db.String, nullable=False) status_category = db.Column(db.Enum(StatusCategory), nullable=False) activity = db.relationship("Activity", backref="status_mappings") __table_args__ = ( db.UniqueConstraint("activity_id", "status"), db.Index( "ix_status_activity_id_null", "status", unique=True, postgresql_where=(activity_id.is_(None)), ), )
self.processor_id = None @property def approved(self): if self.result is None: return None else: return bool(self.result) @approved.setter def approved(self, result): self.result = int(result) def __repr__(self): return u'<Applicant id={0:d}, user={1!r}, reason={2}>'.format( self.id, self.user, self.reason) LastRead = db.Table( 'last_read', db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True), db.Column('thread_id', db.Integer, db.ForeignKey('thread.id'), primary_key=True), db.Column('post_number', db.Integer, db.ForeignKey('post.number')), db.Index('user_thread_index', 'user_id', 'thread_id'))
return { "prod_id": self.prod_id, "quan_in_stock": self.quan_in_stock, "sales": self.sales } t_orderlines = db.Table( 'orderlines', db.Column('orderlineid', db.Integer, nullable=False), db.Column('orderid', db.ForeignKey('orders.orderid', ondelete='CASCADE'), nullable=False), db.Column('prod_id', db.ForeignKey('products.prod_id'), nullable=False), db.Column('quantity', db.SmallInteger, nullable=False), db.Column('orderdate', db.Date, nullable=False), db.Index('ix_orderlines_orderid', 'orderid', 'orderlineid')) class Order(db.Model): __tablename__ = 'orders' orderid = db.Column(db.Integer, primary_key=True, server_default=db.FetchedValue()) orderdate = db.Column(db.Date, nullable=False) customerid = db.Column(db.ForeignKey('customers.customerid', ondelete='SET NULL'), index=True) netamount = db.Column(db.Numeric(12, 2), nullable=False) tax = db.Column(db.Numeric(12, 2), nullable=False) totalamount = db.Column(db.Numeric(12, 2), nullable=False)
class Disease(BioModel): __table_args__ = (db.Index('idx_name', 'name', mysql_length=1024), ) # CLNDBN: Variant disease name name = db.Column(db.Text, nullable=False, unique=True)