Suppose i have 2 tables , Table 1 consist of users info and Table 2 consist of Branch info. table 1 and table 2 is related to each other by many to many relationship. e.g 1 user can work in multiple branches and 1 branch can have multiple users. so here there's no parent child concept. i was wondering if i have to create another table with schema and relate it to user and branch table using foreign key or shall i create an association table.
I have done this :
class UserBranchMap(db.Model):
id = db.Column(db.Integer, primary_key=True)
branch_id = db.Column(db.Integer,db.ForeignKey('branch.id'))
branch = db.relationship("Branch", backref=db.backref("UBMbranch", lazy="dynamic"))
user_id = db.Column(db.Integer,db.ForeignKey('user.id'))
user = db.relationship("User", backref=db.backref("UBMuser", lazy="dynamic"))
created_at = db.Column(db.VARCHAR(20), nullable = False)
created_by = db.Column(db.VARCHAR(20), nullable = False)
class UserBranchMapSchema(ma.Schema):
branch = fields.Nested(branch_schema)
user = fields.Nested(user_schema)
class Meta:
fields = ('branch_id','user_id','created_at','created_by')
userbranchmap_schema = UserBranchMapSchema()
userbranchmaps_schema = UserBranchMapSchema(many = True)
what's the difference between association table and this mapping table ?