Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

python - Flask SQLAlchemy - Seeding with Association Objects

So I'm trying to create the seed files for my project, which involves two association objects (association tables are join tables that have extra data outside of just foreign keys, if I understand it correctly). However, I'm not quite sure on how to set the associations for the seed file, since there's not quite a parent-child relationship that I can tell like the example in the docs.

Basic Layout:

In this, both predictions and comments are many-to-many association objects between users and events

class User(db.Model):
__tablename__ ='users'
... columns here
events = db.relationship('Prediction', back_populates='users')
comments = db.relationship('Comment', back_populates='users')

class Event(db.Model):
__tablename__='events'
...columns here
users = db.relationship('Prediction', back_populates='events')
comments = db.relationship('Comment', back_populates='events')

class Prediction(db.Model):
__tablename__ = 'predictions'
...Columns here (user_id, event_id, choice, probability)
user = db.relationship('User', back_populates='events')
event = db.relationship('Event', back_populates='users')

class Comment(db.Model):
__tablename__ = 'comments'
...Columns here (user_id, event_id, comment)
user = db.relationship('User', back_populates='events')
event = db.relationship('Event', back_populates='users')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...