I'm using flask-sqlalchemy
to build a webapp and I have the following model(s):
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True)
candidates = db.relationship('Candidate', backref='post', lazy='dynamic')
def __init__(self, name):
self.name = name
def __repr__(self):
return "<Post %r>" % self.name
class Candidate(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), primary_key=True)
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
hostel = db.Column(db.String(80))
yes_no = db.Column(db.Boolean)
#votes = db.relationship('Vote', backref='vote', lazy="dynamic")
def __init__(self, name, hostel, post_id, yes_no):
self.name = name
self.hostel = hostel
self.post_id = post_id
self.yes_no = yes_no
def __repr__(self):
return "<Candidate: %r, Post: %r>" % (self.name, self.post.name)
I'm totally noob when it comes to database designing, so I'll really appreciate a few pointers about some fundamental queries I have
Is the
id
column necessary? Most of the tutorials I've seen have that as a column(primary key specifically). Can I do without it? Especially in cases where I have another primary key?How can I have a composite key constraint on Candidate#Name and Candidate#post_id columns, given that post_id is a foreign key.
How can I "refresh" my database. After I've made changes in the model, how can I make sure those changes are reflected in the actual tables? Do I need to
drop_all
andcreate_all
manually?Lastly, does the
sqlalchemy
documentation apply toflask-sqlalchemy
as well?
sqlalchemy
documentation applies toflask-sqlalchemy
as well.