Ludimila
Ludimila

Reputation: 19

Error in query many-to-many relationship in sqlalchemy using flask

I have two models CoreDrive and GamificationTechnique and a generated table of a many-to-many relationship. I'm trying to do a simple query in table cores_techiques , but always get the error.

AttributeError: 'Table' object has no attribute 'query'.

I'm using python3, flask and sqlalchemy. I'm very beginner, I'm trying to learn flask with this application.

Models

#many_CoreDrive_has_many_GamificationTechnique
cores_techiques = db.Table('cores_techiques',
db.Column('core_drive_id', db.Integer, db.ForeignKey('core_drive.id')),
db.Column('gamification_technique_id', db.Integer, db.ForeignKey('gamification_technique.id')))

class CoreDrive(db.Model):
id = db.Column(db.Integer(), primary_key=True)
name_core_drive = db.Column(db.String(80), unique=True)
description_core_drive = db.Column(db.String(255))
techniques = db.relationship('GamificationTechnique', secondary=cores_techiques,
   backref=db.backref('core_drives', lazy='dynamic'))

class GamificationTechnique(db.Model):
id = db.Column(db.Integer(), primary_key=True)
name_gamification_technique = db.Column(db.String(80), unique=True)
description_gamification_technique = db.Column(db.String(255))
number_gamification_technique = db.Column(db.Integer())
attributtes = db.relationship('Atributte', secondary=techniques_atributtes,
   backref=db.backref('gamification_techniques', lazy='dynamic'))

Routes

@app.route('/profile')
def profile():
my_core_techique = cores_techiques.query.all()
my_user = User.query.first()
my_technique = GamificationTechnique.query.all()
return render_template('profile.html',my_user=my_user,my_technique=my_technique, my_core_techique=my_core_techique)

Upvotes: 0

Views: 341

Answers (1)

Jair Perrut
Jair Perrut

Reputation: 1370

You can list all the CoreDriver and each object have a list of the GamificationTechnique

cores = CoreDrivers.query.all()
for core in cores:
    print core.techniques

With this in mind you can spend only the list cores to page

Upvotes: 1

Related Questions