A
A
asantat2018-05-23 23:01:59
Flask
asantat, 2018-05-23 23:01:59

How to adequately bind fields in this data model using SQLAlchemy?

Source:
flask application database
infrastructure - flask-sqlAlchemy + sqlite
features - applied unloaded application with a complex system of data models that are connected in parallel and influence each other, building a dynamic hierarchical system that exists in parallel in several hierarchical states. Relational databases were chosen.

class Attribute(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  attributename= db.Column(db.String(120), index=True, unique=False)
  source= db.Column(db.String(120), index=True, unique=False)
  description = db.Column(db.Text, index=True)
      
class Criteria(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  criteria= db.Column(db.String(64), index=True, unique=True)

class Classification(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  source= db.Column(db.String(120), index=True, unique=False)
  description = db.Column(db.Text, index=True)
      
class Node(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  parent_id = db.Column(db.Integer, db.ForeignKey('node.id'))
  attribute_id = db.Column(db.Integer, db.ForeignKey('attribute.id'))
  criteria_id = db.Column(db.Integer, db.ForeignKey('criteria.id'))
  classification_id = db.Column(db.Integer, db.ForeignKey('classification.id'))
  left = db.Column("lft", db.Integer, index=True, nullable=False)
  right = db.Column("rgt", db.Integer, index=True, nullable=False)
  
  childs = db.relationship('Node', backref=db.backref("parent", remote_side=id),lazy="dynamic")
  attribute= db.relationship('Attribute', backref='node')
  criteria= db.relationship('Criteria', backref='node')
  classification = db.relationship('Classification', backref='node')


The model on which most operations will take place is Node. it also acts as a linking table and carries information about the hierarchy. At the base stage, I want it to work. And then I will add the necessary parameters to use it to organize and display other models. In this revision, the schema has been validated during database initialization and migration, and during the creation of all entities except Node. But an attempt to use the Node model leads to a database error (sqlite3.InterfaceError). Since it does not come out with one associated parameter, I did not register the rest.

How I use this model (flask-wtforms):
class NodeForm(FlaskForm):
  criteria_id = SelectField(u'Criteria', coerce=int)
  submit = SubmitField('Add new')

@app.route('/add-node', methods=['GET', 'POST'])
def addnode():
  criteria = Criteria.query.get(id)
  form = NodeForm(request.POST, obj=criteria)
  form.criteria_id.choices = [(g.id, g.criteria) for g in Criteria.query]
  form = NodeForm()
  if form.validate_on_submit():
    criteria= Node(criteria_id=form.criteria_id.data)
    db.session.add(criteria)
    db.session.commit()
    flash('New Node created!')
    return redirect(url_for('addnode'))
  return render_template('addnode.html', form=form)

What is needed in this form: on the page for adding a new Node entity, display a list of ready-made related Criteria entities, from which select one, the identifier of which will be written in the corresponding field (criteria_id in the Node model).

These are elementary things, I have repeatedly consulted the manuals and documentation, but could not solve the problem of how to describe the relationships in the model so that they can be used.

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