class ModelName(db.Model): __table_args__ = (db.UniqueConstraint('name', 'uid'), db.UniqueConstraint('hid', 'gid')) id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String(80), nullable=False) uid = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) hid = db.Column(db.Integer, db.ForeignKey('table_name.id'), nullable=False) gid = db.Column(db.Integer, db.ForeignKey('table_name.id'), nullable=False) models = db.relationship('Model', backref='name', lazy=True)
class Table(db.Model): __table_args__ = (db.UniqueConstraint('name_id', 'ver'), ) id = db.Column(db.Integer, autoincrement=True, primary_key=True) name_id = db.Column(db.Integer, db.ForeignKey('table_name.id'), nullable=False) ver = db.Column(db.Integer, nullable=False, default=1) time = db.Column(db.DateTime, default=datetime.datetime.utcnow) id_col = db.Column(db.String(10), default='id') y_col = db.Column(db.String(10)) y_col_id = db.Column(db.Integer, default=-1) memo = db.Column(db.String(320), default='') price0 = db.Column(db.Numeric(10, 2), default=.01) price1 = db.Column(db.Numeric(10, 2), default=.01) price2 = db.Column(db.Numeric(10, 2), default=.01) score = db.Column(db.Float) # name backref from TableName def get_fate_path(self): return 't%d_v%d' % (self.name_id, self.ver) def get_local_path(self): path = 'u%d_t%d_v%d' % (self.name.uid, self.name_id, self.ver) return os.path.join(app.config['UPLOAD_FOLDER'], path)
class GroupMember(db.Model): id = db.Column(db.Integer, primary_key=True) group_id = db.Column(db.Integer, db.ForeignKey('group.id'), nullable=False) member_id = db.Column(db.Integer, db.ForeignKey('member.id'), nullable=False) __table_args__ = (db.UniqueConstraint('group_id', 'member_id'), )
class SizeResult(db.Model): __table_args__ = (db.UniqueConstraint('hid', 'gid'), ) id = db.Column(db.Integer, autoincrement=True, primary_key=True) hid = db.Column(db.Integer, db.ForeignKey('table.id'), nullable=False) gid = db.Column(db.Integer, db.ForeignKey('table.id'), nullable=False) size = db.Column(db.Integer) host_table = db.relationship("Table", foreign_keys=[hid]) guest_table = db.relationship("Table", foreign_keys=[gid])
class Model(db.Model): __table_args__ = ( db.UniqueConstraint('name_id', 'ver'), db.UniqueConstraint('gid', 'hid'), ) id = db.Column(db.Integer, autoincrement=True, primary_key=True) name_id = db.Column(db.Integer, db.ForeignKey('model_name.id'), nullable=False) gid = db.Column(db.Integer, db.ForeignKey('table.id'), nullable=False) hid = db.Column(db.Integer, db.ForeignKey('table.id'), nullable=False) time = db.Column(db.DateTime, default=datetime.datetime.utcnow) ver = db.Column(db.Integer, nullable=False) memo = db.Column(db.String(320)) # name: backref from ModelName def get_fate_path(self): return 'm%d_v%d' % (self.name_id, self.ver)
class Sample(db.Model): __tablename__ = 'samples' __table_args__ = (db.UniqueConstraint('sample_name', 'user_id', name='sample_id'), ) id = db.Column(db.Integer, primary_key=True, autoincrement=True) sample_name = db.Column(db.String) user_id = db.Column(db.String, db.ForeignKey('users.user_id')) files = db.relationship(File, secondary=sample_file_link, backref="samples") runs = db.relationship(Run, backref="sample")
class Location(db.Model): """SqlAlchemy model class representing a hierarchical relations between actual physical locations. Attributes ---------- common_name : str well-known name of the location latitude : float coordinate value longitude : float coordinate value parent_id : int foreign key of parent location parent : Location parent location location_external : list associated LocationExternal objects Args ---- common_name : str well-known name of the location latitude : float coordinate value longitude : float coordinate value parent : Location parent location **kwargs : dict variable-length args to pass to superclass """ __tablename__ = 'location' id = db.Column(db.Integer, primary_key=True) __table_args__ = (db.UniqueConstraint('common_name', 'parent_id', name='location_path_unique_idx'), ) common_name = db.Column(db.String()) latitude = db.Column(db.Numeric) longitude = db.Column(db.Numeric) parent_id = db.Column(db.Integer, db.ForeignKey('location.id')) parent = db.relationship('Location', remote_side=[id]) location_external = db.relationship('LocationExternal', lazy=True) def set_parent(self, parent): """Set parent location """ if self.parent != None: raise ValueError('parent already set') self.parent = parent def add_external_data(self, source, references_data): """Create new external data source for location Parameters ---------- source : enum external source identifier references_data : dict external source data """ self.location_external.append( LocationExternal(self, source, references_data)) def is_same_external_data(self, source, references_data): """see LocationExternal.is_same() Parameters ---------- source : enum external source identifier references_data : dict external source data Returns ------- match : bool """ if len(self.location_external) == 0: return False return self.location_external[0].is_same(source, references_data) @staticmethod def get_by_custom(source_enum, key, value): """see also LocationExternal.get_by_custom() Parameters ---------- source_enum : enum external source identifier key : str key to match value : str value to match Returns ------- external : list list of matches external objects """ r = LocationExternal.get_by_custom(source_enum, key, value) if len(r) == 0: return None l = Location.query.get(r[0].location_id) return l def get_custom(self, source_enum): """Returns the extended reference of a location for the given type enum Parameters ---------- source_enum : enum external source identifier Returns ------- external_reference : dict the matching reference values """ for external in self.location_external: if external.source == source_enum.name: return external.external_reference def __init__(self, common_name, latitude, longitude, parent=None, **kwargs): super(Location, self).__init__(**kwargs) self.common_name = common_name self.latitude = latitude self.longitude = longitude if parent != None: self.set_parent(parent) def __str__(self): location_string = [] location = self while location != None: location_string.append(location.common_name) location = location.parent return ", ".join(location_string)
from server import db from datetime import datetime timed_comment_likes = db.Table('timed_comment_likes', db.Column('user_id',db.Integer(),db.ForeignKey('User.id')), db.Column('comment_id',db.Integer(),db.ForeignKey('TimedComment.id')), db.UniqueConstraint('user_id', 'comment_id', name='like_constraint') ) class TimedComment(db.Model): __tablename__ = 'TimedComment' id = db.Column(db.Integer, primary_key = True) show_series_id = db.Column(db.Integer, db.ForeignKey('ShowSeries.id')) user_id = db.Column(db.Integer, db.ForeignKey('User.id')) comment_time = db.Column(db.Integer) is_reported = db.Column(db.Boolean()) content = db.Column(db.String(200)) post_date = db.Column(db.DateTime(), default = datetime.utcnow) parent_id = db.Column(db.Integer, db.ForeignKey('TimedComment.id')) liked_by = db.relationship('User', secondary=timed_comment_likes, backref=db.backref('liked_timed_comments',lazy='dynamic'), lazy='dynamic') children = db.relationship("TimedComment", backref = db.backref('parent',remote_side = id)) def __init__(self, show_series_id, user_id, comment_time, content, parent_id): self.show_series_id = show_series_id self.user_id = user_id self.comment_time = comment_time self.content = content self.parent_id = parent_id
class TableName(db.Model): __table_args__ = (db.UniqueConstraint('uid', 'name'), ) id = db.Column(db.Integer, autoincrement=True, primary_key=True) uid = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) name = db.Column(db.String(80), nullable=False) tables = db.relationship('Table', backref='name', lazy=True)
from server import db from datetime import datetime role_email = db.Table( 'role_email', db.Column('role_id', db.Integer(), db.ForeignKey('Roles.role_id')), db.Column('email_id', db.Integer(), db.ForeignKey('Emails.email_id')), db.UniqueConstraint('role_id', 'email_id', name='role_email_constraint')) class Roles(db.Model): __tablename__ = 'Roles' role_id = db.Column(db.Integer, primary_key=True) role_name = db.Column(db.String(100), unique=True) edit_date = db.Column(db.DateTime(), onupdate=datetime.utcnow, default=datetime.utcnow) emails = db.relationship('Emails', secondary=role_email, backref=db.backref('roles', lazy='dynamic'), lazy='dynamic') def __init__(self, role_name): self.role_name = role_name def add_email(self, email): if not self.has_email(email): self.emails.append(email)