给出一个类似于SQLAlchemy文档(http://docs.sqlalchemy.org/en/rel_1_0/orm/tutorial.html#building-a-relationship)中的关系模式:
from sqlalchemy import *
from sqlalchemy.orm import relationship
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
class Address(Base):
__tablename__ = 'address'
id = Column(Integer, primary_key=True)
email_address = Column(String)
user_id = Column(Integer, ForeignKey('user.id'))
user = relationship('User', backref='addresses')
有没有办法限制User
个实例必须至少有一个Address
?
换句话说,这应该失败:
user = User()
session.add(user)
session.commit()
......虽然这应该成功:
user = User()
address1 = Address(email_address='example1@example.com')
address2 = Address(email_address='example2@example.com')
user.addresses = [address1, address2]
session.add(user)
session.commit()