I have trouble linking two tables with 'Posts' and 'Comments' so comments are only displayed on particular post, where they have been created.
With linking post and user I used current_user.id to make link between tables, but using ForeignKey gaves me always error:
sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship Post.post_rel - there are no foreign keys linking these tables
Below is my code:
class Post(db.Model):
__tablename__ = 'post'
id = db.Column(Integer, primary_key=True)
title = db.Column(String(50))
subtitle = db.Column(String(50))
author = db.Column(String(20))
date_posted = db.Column(DateTime)
content = db.Column(Text)
post_rel = relationship('Post', back_populates='comment_rel', foreign_keys='[Comment.post_id]')
def get_comments(self):
return Comments.query.filter_by(post_id=post.id).order_by(Comments.timestamp.desc())
def __repr__(self):
return '<Post %r>' % (self.body)
class Comment(db.Model):
__tablename__ = 'comment'
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String(140))
author = db.Column(db.String(32))
timestamp = db.Column(db.DateTime(), default=datetime.utcnow, index=True)
post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False)
comment_rel = relationship('Comment', uselist=False, back_populates='post_rel')
def __init__(self, text, author, timestamp):
""""""
self.text = text
self.author = author
self.timestamp = timestamp
def __repr__(self):
return '<Post %r>' % (self.body)
def show(self):
return self.author + '\n' + self.text
In your relationship, you have to change the name of the tables.
post_rel = relationship('Comment', back_populates='comment_rel',
foreign_keys='[Comment.post_id]')
comment_rel = relationship('Post', uselist=False,
back_populates='post_rel')
I have corrected your code:
BaseModel = declarative_base()
class Post(BaseModel):
__tablename__ = 'post'
id = Column(Integer, primary_key=True)
title = Column(String(50))
subtitle = Column(String(50))
author = Column(String(20))
post_rel = relationship('Comment', back_populates='comment_rel', foreign_keys='[Comment.post_id]')
class Comment(BaseModel):
__tablename__ = 'comment'
id = Column(Integer, primary_key=True)
text = Column(String(140))
author = Column(String(32))
comment_rel = relationship('Post', uselist=False, back_populates='post_rel')
Related
So i been trying to make a like function for my q&a website. however, i'm stuck on database relations part of the models.py. I'm getting an error that says
"sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'mapped class User->user'. Original exception was: Could not determine join condition between parent/child tables on relationship User.posts - there are multiple foreign key paths linking the tables. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference to the parent table."
This is my code for the user and post class
class Post(db.Model):
id = db.Column("id", db.Integer, primary_key=True)
title = db.Column("title", db.String(200))
text = db.Column("text", db.String(100))
date = db.Column("date", db.String(50))
#Create Foreign Key
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
comments = db.relationship("Comment", backref="post", cascade="all, delete-orphan", lazy=True)
recipient_id = db.Column(db.Integer, db.ForeignKey('user.id'))
likes = db.relationship('PostLike', backref='post', lazy='dynamic')
and my user class
class User(db.Model):
id = db.Column("id", db.Integer, primary_key=True)
first_name = db.Column("first_name", db.String(100))
last_name = db.Column("last_name", db.String(100))
email = db.Column("email", db.String(100))
password = db.Column(db.String(255), nullable=False)
registered_on = db.Column(db.DateTime, nullable=False)
posts = db.relationship("Post", backref="user", lazy=True)
comments = db.relationship("Comment", backref="user", lazy=True)
liked = db.relationship(
'PostLike',
foreign_keys='PostLike.user_id',
backref='user', lazy='dynamic'
)
def like_post(self, post):
if not self.has_liked_post(post):
like = PostLike(user_id=self.id, post_id=post.id)
db.session.add(like)
def unlike_post(self, post):
if self.has_liked_post(post):
PostLike.query.filter_by(
user_id=self.id,
post_id=post.id).delete()
def has_liked_post(self, post):
return PostLike.query.filter(
PostLike.user_id == self.id,
PostLike.post_id == post.id).count() > 0
my postlike class in the models.py
class PostLike(db.Model):
__tablename__ = 'post_like'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
my flask file for like_action function
#app.route('/like/<int:post_id>/<action>')
def like_action(post_id, action):
post = Post.query.filter_by(id=post_id).first_or_404()
if action == 'like':
session['user_id'].like_post(post)
db.session.commit()
if action == 'unlike':
session['user_id'].unlike_post(post)
db.session.commit()
return redirect(request.referrer)
You have two foreign keys pointing to User on Post:
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
recipient_id = db.Column(db.Integer, db.ForeignKey('user.id'))
so, your User doesn't know where to point
posts = db.relationship("Post", backref="user", lazy=True)
Have something like recipient = db.relationship (..) and author = db.relationship (..) in User model, and make posts = db.relationship("Post", back_populates="author", lazy=True).
I'm looking to have a 'many-to-many' relationship between users and products. For this, I prepared another table product_user but it is not working well. I can't use it in secondary relationship.
Error:
NameError: name 'product_users' is not defined
This is code:
## Product model
class Product(db.Model):
__tablename__ = 'products'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
users = db.relationship("User", secondary=product_users, backref="users", lazy="dynamic")
def __repr__(self):
return '<Product %r>' % self.uid
## User model
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
products = db.relationship("Product", secondary=product_users, backref="products", lazy="dynamic")
def __repr__(self):
return '<User %r>' % self.uid
## Product User model
class ProductUser(db.Model):
__tablename__ = 'product_users'
id = db.Column(db.Integer, primary_key=True)
product_id = db.Column(db.Integer,db.ForeignKey('products.id'))
user_id = db.Column(db.Integer,db.ForeignKey('users.id'))
product = db.relationship("Product", backref='products', foreign_keys=[product_id])
user = db.relationship("User", backref='users', foreign_keys=[user_id])
def __repr__(self):
return '<ProductUser %r>'
Sorry I have typo table name.
from:
users = db.relationship("User", secondary=product_users, backref="users", lazy="dynamic")
products = db.relationship("Product", secondary=product_users, backref="products", lazy="dynamic")
to:
users = db.relationship("User", secondary='product_users', backref="users", lazy="dynamic")
products = db.relationship("Product", secondary='product_users', backref="products", lazy="dynamic")
my code for db in Flask and SQLalchemy is working but when I make comment in post, ForeignKey doesn't link comment with that post and there is no value for post_id in table "comment" when comment is created.
Do you please know, what could be wrong? Bellow are two ways how I tried to create db, but none has made connections with ForeingKey.
Tryed using Pragma to force foreignkey, but it didnt help. Tried to enforce ForeignKey on with sqlite> PRAGMA foreign_keys = ON; but it does not persist.
Here is my code:
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///userdata.db'
db = SQLAlchemy(app)
engine = create_engine('sqlite:///userdata.db')
def _fk_pragma_on_connect(dbapi_con, con_record):
dbapi_con.execute('pragma foreign_keys=ON')
from sqlalchemy import event
event.listen(engine, 'connect', _fk_pragma_on_connect)
class Post(db.Model):
__tablename__ = 'post'
id = db.Column(Integer, primary_key=True)
title = db.Column(String(50))
subtitle = db.Column(String(50))
author = db.Column(String(20))
date_posted = db.Column(DateTime)
content = db.Column(Text)
post_rel = relationship('Comment',
back_populates='comment_rel',
foreign_keys='[Comment.post_id]')
def get_comments(self):
return Comments.query.filter_by(
post_id=post.id).order_by(Comments.timestamp.desc())
def __repr__(self):
return '<Post %r>' % (self.body)
class Comment(db.Model):
__tablename__ = 'comment'
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String(140))
author = db.Column(db.String(32))
timestamp = db.Column(db.DateTime(), default=datetime.utcnow, index=True)
post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False)
comment_rel = relationship('Post', uselist=False, back_populates='post_rel')
def __init__(self, text, author, timestamp):
""""""
self.text = text
self.author = author
self.timestamp = timestamp
def __repr__(self):
return '<Post %r>' % (self.body)
def show(self):
return self.author + '\n' + self.text
#app.route('/addcomment', methods=['POST'])
def addcomment():
commentar = request.form['commentar']
comment_done = Comment(text=commentar, author=current_user.username,
timestamp=datetime.now())
db.session.add(comment_done, 'PRAGMA foreign_keys=on')
db.session.commit()
return render_template('post.html', post=post)
I have the following badge (achievement) system database structure:
class Base(db.Model):
__abstract__ = True
id = db.Column(db.Integer, primary_key=True)
date_created = db.Column(db.DateTime, default=db.func.current_timestamp())
date_modified = db.Column(db.DateTime, default=db.func.current_timestamp(),
onupdate=db.func.current_timestamp())
class User(UserMixin, Base):
__tablename__ = 'users'
username = db.Column(db.String(20), nullable=False, unique=True)
email = db.Column(db.String(50), nullable=False, unique=True)
password = db.Column(db.String(200), nullable=False)
name = db.Column(db.String(30), nullable=False)
badges = db.relationship('UserBadge', backref='ubadge',
lazy='dynamic')
class Badge(Base):
__tablename__ = 'badges'
name = db.Column(db.String(35), unique=True)
description = db.Column(db.String(300))
imgfile = db.Column(db.String(80))
badges = db.relationship('UserBadge', backref='badge',
lazy='dynamic')
def __repr__(self):
return '<Achievement: {} - {}>'.format(self.name, self.description)
class UserBadge(Base):
__tablename__ = 'userbadges'
user_id = db.Column(db.Integer(), db.ForeignKey('users.id'))
badge_id = db.Column(db.Integer(), db.ForeignKey('badges.id'))
def __repr__(self):
return '<Achievement: {} - {}>'.format(self.user_id, self.badge_id)
So i can return all the badges by a specific user, using:
ubadges = UserBadge.query.filter_by(user_id=user.id).all()
It returns:
[<Achievement: 1 - 1>]
But instead of 1 (user_id) and 1 (badge_id) i want to show the users.name and badges.name. How can i access those attributes?
In your UserBadge class, just use:
def __repr__(self):
return '<Achievement: {} - {}>'.format(self.ubadge.name, self.badge.name)
It has both properties because you set them up using backref in the other classes.
PS: You might need to change the User backref to user, and then use self.user.name int he function above
users model
class User(db.Model):
__tablename__ = "users"
id = db.Column('user_id',db.Integer , primary_key=True)
username = db.Column('username', db.String(20), unique=True , index=True)
password = db.Column('password' , db.String(250))
posts = db.relationship('Post', backref = 'user', lazy = 'dynamic')
def __init__(self , username ,password ):
self.username = username
self.password = password
posts model
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(140))
text = db.Column(db.String(2000))
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
def __init__(self, title, text):
self.title = title
self.text = text
now I want to add comments model but dont know how to do it .please help me in comments view also .
comments should be connected to user and post .
Thank You
I solved this myself
here is the comments model
class Comment(db.Model):
__tablename__ = 'comments'
id = db.Column(db.Integer, primary_key = True)
text = db.Column(db.String(2000))
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))
post_id = db.Column(db.Integer, db.ForeignKey('posts.post_id'))
def __init__(self, text):
self.text = text