简体   繁体   English

创建表时Sqlalchemy外键关系错误

[英]Sqlalchemy Foreign key relationship error while creating tables

Im creating tables using sqlalchemy and some of these tables has more than one foreign key relationship.Below is the code.我使用 sqlalchemy 创建表,其中一些表有多个外键关系。下面是代码。 I'm getting error:我收到错误:

sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'project' and 'genre' sqlalchemy.exc.NoForeignKeysError:找不到“项目”和“流派”之间的任何外键关系

when try to run the script.尝试运行脚本时。

Could anyone explain me how tables are created in sqlalchemy, order of the creation if there are tables with more than one relation.任何人都可以向我解释如何在 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:newpassword@localhost: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()

Error错误

"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." “sqlalchemy.exc.NoForeignKeysError:无法确定关系Project.genre_id的父/子表之间的连接条件> - 没有链接这些表的外键。确保引用列与ForeignKey或ForeignKeyConstraint相关联,或指定'primaryjoin ' 表达。”

You're overwriting the column specification with your relationship objects, so SQLAlchemy never sees the columns.您正在用关系对象覆盖列规范,因此 SQLAlchemy 永远不会看到这些列。 See if your database doesn't have the foreign key columns in tables and rename either the column or relationship class variables to have unique names.查看您的数据库是否在表中没有外键列,并将列或关系类变量重命名为具有唯一名称。 Maybe something like this:也许是这样的:

owner_id = Column(Integer,ForeignKey('users.id'))
owner = relationship('Users',backref = 'users')

In my case i wrote the table but forgot the column:就我而言,我写了表格但忘记了列:

owner_id = Column(Integer, ForeignKey('users'))

Instead of代替

owner_id = Column(Integer, ForeignKey('users.id'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM