D
D
dbelikov2015-02-06 17:40:39
Flask
dbelikov, 2015-02-06 17:40:39

What is the correct way to create a database model in Flask in combination with SQLAlchemy?

Good afternoon,
I have the following database model

class Sensor(db.Model):
    __tablename__ = 'sensor'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), index=True, unique=False)
    unit = relationship('Unit')


class Value(db.Model):
    __tablename__ = 'value'
    id = db.Column(db.Integer, primary_key=True)
    value = db.Column(db.Float, index=False, unique=False)
    timestamp = db.Column(db.DateTime)
    sensor_id = db.Column(db.Integer, db.ForeignKey('sensor.id'))
    sensor = db.relationship('Sensor', backref='value', lazy = 'select')

class Unit(db.Model):
    __tablename__ = 'unit'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(32), index=True, unique=False)
    description = db.Column(db.String(32), index=True, unique=False)

in the code I make a request for the values ​​​​for a specific sensor.
db_result = Value.query.filter(Value.sensor_id == sensor_id).limit(100).all()

how to correctly specify db.relationship so that you can ask like this:
db_result.sensor.unit.description
?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question