Sqlalchemy创建表时出现外键关系错误

时间:2014-09-16 06:07:36

标签: python sqlalchemy flask-sqlalchemy sqlalchemy-migrate

我使用sqlalchemy创建表,其中一些表有多个外键关系。下面是代码。我得到错误:

  

sqlalchemy.exc.NoForeignKeysError:无法找到任何外键   项目'之间的关系和'流派'

尝试运行脚本时。

有没有人可以解释我如何在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()

错误

  

" sqlalchemy.exc.NoForeignKeysError:无法确定加入条件>关系Project.genre_id上​​的父/子表之间 - 没有链接这些表的外键。确保引用列与ForeignKey或ForeignKeyConstraint相关联,或指定' primaryjoin' 。表达"

2 个答案:

答案 0 :(得分:4)

您使用关系对象覆盖列规范,因此SQLAlchemy永远不会看到列。查看您的数据库是否在表中没有外键列,并将列或关系类变量重命名为具有唯一名称。也许是这样的:

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

答案 1 :(得分:0)

就我而言,我写了表格但忘记了列:

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

代替

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