-4

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

user3891081
  • 93
  • 1
  • 7
  • if you dont want to answer no problem but please dont down vote it .let someone else answer – user3891081 Sep 17 '17 at 09:29
  • https://stackoverflow.com/questions/8112831/implementing-comments-and-likes-in-database – Nabin Sep 17 '17 at 13:58
  • https://www.google.com.np/search?q=how+is+coment+table+designed+along+with+user+and+post+table&oq=how+is+coment+table+designed+along+with+user+and+post+table&aqs=chrome..69i57.20678j0j7&sourceid=chrome&ie=UTF-8 – Nabin Sep 17 '17 at 13:58
  • Hope you have some ideas from these links. Good luck – Nabin Sep 17 '17 at 13:58

1 Answers1

0

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   
user3891081
  • 93
  • 1
  • 7