数据库
首页 > 数据库> > Build a CRUD App with SQLAlchemy - Foreign Key Constraint Setup

Build a CRUD App with SQLAlchemy - Foreign Key Constraint Setup

作者:互联网

db.relationship


CREATE TABLE vehicles (
	id INTEGER PRIMARY KEY,
	make VARCHAR NOT NULL,
	model VARCHAR NOT NULL,
	year INTEGER NOT NULL,
	driver_id REFERENCES drivers(id)
)

driver_id maps to an id that belongs in the drivers table.
driver_id is nonempty


db.ForeignKey

class SomeParent(db.Model):
	__tablename__ = 'some_parents'
	id = db.Column(db.Integer, primary_key=True)
	...

class SomeChild(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(50), nullable=False)
	#some_parents.id: Maps to parent table's primary key
	# format: {parent tablename}.{parent primary key column name}
	some_parent_id = db.Column(db.Integer, db.ForeignKey('some_parents.id'), nullable=False)

SQLAlchemy foreign keys Docs

标签:SQLAlchemy,parent,Constraint,some,App,db,foreign,key,id
来源: https://blog.csdn.net/BSCHN123/article/details/121561470