sqlalchemy를 사용하여 테이블을 만드는 중 일부 테이블에는 외래 키 관계가 두 개 이상 있습니다. 아래 코드가 있습니다. 오류 :Sqlalchemy 테이블을 만들 때 외래 키 관계 오류
sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'project' and 'genre'
스크립트를 실행하려고하면 오류가 발생합니다.
아무도 나에게 하나 이상의 관계가있는 테이블이있는 경우 생성 순서를 sqlalchemy에서 어떻게 작성하는지 설명 할 수 있습니까?
import sqlalchemy
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
import datetime
from sqlalchemy import *
engine = sqlalchemy.create_engine('postgresql+psycopg2://postgres:[email protected]:5432/test');
Session = sessionmaker(bind=engine)
session = Session()
metadata = MetaData(engine)
metadata.drop_all(engine)
Base = declarative_base(metadata=metadata)
class User(Base):
__tablename__ = 'users'
id = Column(Integer,primary_key = True)
email = Column(String(30),nullable = False)
password = Column(String(30),nullable = False)
class Genre(Base):
__tablename__ = 'genre'
id = Column(Integer,primary_key = True)
title = Column(String(30),nullable = False)
class Status(Base):
__tablename__ = 'status'
id = Column(Integer,primary_key = True)
status = Column(String(30),nullable = False)
class FilmType(Base):
__tablename__ = 'filmtype'
id = Column(Integer,primary_key = True)
filmtype = Column(String(30),nullable = False)
class Project(Base):
__tablename__ = 'project'
id = Column(Integer,primary_key = True)
name = Column(String(30),nullable = False)
poster = Column(Text , nullable = False)
genre_id = Column(Integer,ForeignKey('genre.id'))
genre_id = relationship('Genre',backref = 'genres')
owner = Column(Integer,ForeignKey('users.id'))
owner = relationship('Users',backref = 'users')
class Videos(Base):
__tablename__ = 'videos'
id = Column(Integer,primary_key = True)
dropbox_url = Column(Text)
film_type = Column(Integer,ForeignKey('filmtype.id'))
film_type = relationship('FilmType',backref = 'film_types')
video_status = Column(Integer,ForeignKey('status.id'))
video_status = relationship('Status',backref = 'video_statuses')
project_id = Column(Integer,ForeignKey('project.id'))
project_id = relationship('Project',backref = 'projetcs')
user_id = Column(Integer,ForeignKey('users.id'))
user_id = relationship('User' , backref = 'video_owners')
metadata.create_all(bind = engine)
user_ = User(email='lala.com',password='lala')
genre_ = Genre(title='Horror')
status_ = Status(status='Active')
filmtype_ = FilmType(filmtype ='lala')
project_ = Project(name='lala',poster='mij.jpg')
videos_ = Videos(dropbox_url='drop/vi.com')
session.add(videos_)
session.flush()
session.commit()
SQLAlchemy의가 열을 볼 수 없습니다, 그래서 당신은, 당신의 관계 물체로 열 사양을 덮어하고
"sqlalchemy.exc.NoForeignKeysError: Could not determine join condition > between parent/child tables on relationship Project.genre_id - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression."