我是 SQLAlchemy 的新手,我正在尝试使用 SQLAlchemy 构建一个练习项目。我创建了包含具有以下关系的表的数据库。现在我的问题是:
- 如何将数据插入到表中,因为它们是相互依赖的?
- 这会在数据库设计中形成循环吗?
循环数据库设计是一种不好的做法吗?如果这是一种不好的做法,如何解决这个问题?
Department.manager_ssn ==> Employee.SSN
和
Employee.department_id ==> Department.deptid
以下是创建这个确切数据库的代码的当前版本。
# Department table
class Departments(Base):
__tablename__ = "Departments"
# Attricutes
Dname= Column(String(15), nullable=False)
Dnumber= Column(Integer, primary_key=True)
Mgr_SSN= Column(Integer, ForeignKey('Employees.ssn'), nullable=False)
employees = relationship("Employees")
# Employee table
class Employees(Base):
__tablename__ = "Employees"
# Attributes
Fname = Column(String(30), nullable=False)
Minit = Column(String(15), nullable=False)
Lname = Column(String(15), nullable=False)
SSN = Column(Integer, primary_key=True)
Bdate = Column(Date, nullable=False)
Address = Column(String(15), nullable=False)
Sex = Column(String(1), default='F', nullable=False)
Salary = Column(Integer, nullable=False)
Dno = Column(Integer, ForeignKey('Departments.Dnumber'), nullable=False)
departments = relationship("Departments")
请仅在 SQLAlchemy 中提供解决方案,而不是在 flask-sqlalchemy 或 flask-migrate 中提供,我使用的是 Python 3.6。