How can flask do multi-table queries without foreign keys?

< H1 > user table < / H1 >

class User (db.Model):

id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(40), unique=True)  
password = db.Column(db.String(128)) 
< H1 > order form < / H1 >

class User (db.Model):

id = db.Column(db.Integer, primary_key=True)
buy_id = db.Column(db.Integer) -sharp ID  
sell_id = db.Column(db.Integer) -sharp ID
status = db.Column(db.Integer) -sharp
< H1 > SKU < / H1 >

class Sku (db.Model):

id = db.Column(db.Integer, primary_key=True)
sku_name = db.Column(db.String(40))  
money = db.Column(db.Float(20))

description: a user can be a seller or a buyer.

here are three tables. There is no foreign key association. You need to query according to the order status (for example: 0: closed, 1: waiting for payment, 3: waiting for delivery.). At the same time, the seller and product name corresponding to the order need to be displayed in the template.

I thought for a moment that it might be more complicated to set a foreign key. I don"t know which way is easier.


if I remember correctly, sqlalchemy requires that there must be a foreign key. So flask-sqlalchemy is no exception.

Menu