SQLAlchemy에 익숙하지 않아 SQLAlchemy를 사용하여 연습 프로젝트를 작성하려고합니다. 다음과 같은 관계로 테이블을 포함하는 데이터베이스를 만들었습니다. 이제 내 질문은 :SQLAlchemy (flask_sqlalchemy 또는 flask-merge가 아님)를 사용하여 상호 관계 테이블에 데이터를 삽입하는 방법은 무엇입니까?
- 방법 INSERT 데이터를 테이블에가 상호 의존적으로?
- 이것은 데이터베이스 디자인에서 루프를 형성합니까?
루핑 데이터베이스 디자인이 좋지 않습니까? 나쁜 행동이라면 어떻게 해결할 수 있습니까?
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의 또는 플라스크 - 마이그레이션에 SQLAlchemy의에서 솔루션을 제공하십시오 나는 파이썬 3.6 사용하고 있습니다.
는 난 당신이 루프 관계에 대한이 답변, https://stackoverflow.com/a/10458105/4995451을 읽어야 생각 하지만 당신은 당신이 널 (NULL) = TRUE ''과의 관계를 만들 수있는이 구조를 유지하려는 경우. 그래서 당신은 다른 테이블 앞에 하나의 테이블을 삽입 할 수 있습니다 –
@ faisalburhanudin, 내 문제를 해결해 줘서 고마워. – Sunil