Reputation: 1
So I am working on a forum-like website for discussions using the python framework Flask. I set up a database using SQLite. I defined two tables (classes) and made primary keys for both. However, the primary keys they produce are the same. For example, for the first set of data, the primary key is 1 for both and I can't differentiate between the two sets of data. I can access the first table's data if I need to, but when I try to get the second table's data, it gives me the first tables data again. Is there any way to have two unique primary keys for both or do I have to set up two different databases.
from datetime import datetime
from flaskblog import db, login_manager
from flask_login import UserMixin
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
password = db.Column(db.String(60), nullable=False)
posts = db.relationship('Post', backref='author', lazy=True)
posts_addmath = db.relationship('PostAddmath', backref='author', lazy=True)
def __repr__(self):
return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
content = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def __repr__(self):
return f"Post('{self.title}', '{self.date_posted}')"
class PostAddmath(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
content = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def __repr__(self):
return f"Post('{self.title}', '{self.date_posted}')"
These are the tables in models.py.
id_addmath = db.Column(db.Integer, primary_key=True)
I tried changing the 'id' variable in the 'PostAddmath' class but that didn't work.
@app.route("/post/<int:post_id>")
@login_required
def post_intmath(post_id):
post = Post.query.get_or_404(post_id)
return render_template('subjects/intmath/post_intmath.html', title=post.title, post=post)
@app.route("/post/<int:post_id>")
@login_required
def post_addmath(post_id):
post = PostAddmath.query.get_or_404(post_id)
return render_template('subjects/addmath/post_addmath.html', title=post.title, post=post)
This is how a access the data in my routes.py.
When I try to access the data using 'post.id' it gives me only the first tables results not the send tables.
If I have to set up a different database please let me know how to do so. Any help is greatly appreciated. Thank You.
Upvotes: 0
Views: 146
Reputation: 709
You have the same route to both post_intmath
and to post_addmath
you have to change the routes for example the first route keep it as it is /post/<int:post_id>
and the second route change it to /addpost/<int:post_id>
for example.
Upvotes: 1