Following is the Topic Model:
class Topic(db.Model):
__tablename__ = 'topics'
topic_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
topic_name = db.Column(db.String(200), unique=True, nullable=False)
posts = db.relationship('Association', back_populates='topic')
Following is the Post model:
class Post(db.Model):
__tablename__ = 'posts'
post_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
title = db.Column(db.String(200), nullable=False)
content = db.Column(db.Text, nullable=False)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
likes = db.Column(db.Integer, default=0)
dislikes = db.Column(db.Integer, default=0)
author_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
author = db.relationship('User', back_populates='posts')
topics = db.relationship('Association', back_populates='post')
Following is the Association Model:
class Association(db.Model):
__tablename__ = 'post_topic_assoc'
post_id = db.Column('post_id', db.ForeignKey('posts.post_id'), primary_key=True)
topic_id = db.Column('topic_id', db.ForeignKey('topics.topic_id'), primary_key=True)
post = db.relationship('Post', back_populates='topics')
topic = db.relationship('Topic', back_populates='posts')
Here is my view function:
def create(user_id):
form = PostForm()
query = db.session.query(Topic.topic_id, Topic.topic_name)
topics = query.all()
form.topics.choices = topics
if request.method == 'POST' and form.validate_on_submit():
choice_instances = list(map(Topic.query.get, form.topics.data))
new_post = Post(title=form.title.data,
content=form.content.data,
author=current_user)
new_post.topics.extend(choice_instances)
db.session.add(new_post)
db.session.commit()
flash('You post has been created')
return redirect(url_for('blueprint_user.user_home',
user_id=current_user.user_id))
The error just says this:
KeyError: 'post'
I execution does stops at new_post.topics.extend(choice_instances)
line.
Why am I unable to insert the topics using .extend()? Is anything wrong with the way I created the many-many relationship?
Please click here for the complete traceback.
Most of the SO questions (like this) that I came across were using the association table method and not association model. Even in those the .append() or .extend() is working but not in my code.
Please guide me I am a beginner in SqlAlchemy.