ORM SQLAlchemy:关系不存在。如何设置会话连接?

时间:2019-06-04 03:43:14

标签: python orm sqlalchemy

我想使用我的postgreSQL连接使用ORM模块进行批量插入。我研究如何使用session连接数据库。运行时,它会给出

错误代码:     关系\“ user_fav_books \”不存在\ nLINE 1

这是我的代码。我对此一无所知。即使我绑定了引擎,您能否告诉我如何设置会话连接?

 engine = create_engine('postgresql://.....=require' , echo=True)
  metadata = MetaData(engine)  
  Base = declarative_base(metadata=metadata)
  class UserFavBooks(Base):
    __tablename__ = 'user_fav_books'
    id = Column(String, primary_key=True)
    user_label = Column(String)
    book_label = Column(String)
    is_free_assigned = Column(Boolean , default=True)
    free_assign_date = Column(DateTime(timezone=True), default=func.now())
    free_assign_end_date = Column(DateTime(timezone=True), default=func.now())



  session = sessionmaker(bind=engine)()
  session.connection()

  session.add_all(
        [
            UserFavBooks(
                user_label="user02",
                book_label="AKA6",
                is_free_assigned=True,
                free_assign_date= DT.date.today() + DT.timedelta(days=7*0),
                free_assign_end_date= DT.date.today() + DT.timedelta(days=7*1)                
            ),
            UserFavBooks(
                user_label="user02",
                book_label="AKS6",
                is_free_assigned=True,
                free_assign_date= DT.date.today() + DT.timedelta(days=7*2),
                free_assign_end_date= DT.date.today() + DT.timedelta(days=7*3)                
            ),
            UserFavBooks(
                user_label="user02",
                book_label="AK46",
                is_free_assigned=True,
                free_assign_date= DT.date.today() + DT.timedelta(days=7*4),
                free_assign_end_date= DT.date.today() + DT.timedelta(days=7*5)                
            ),
            UserFavBooks(
                user_label="user02",
                book_label="AK26",
                is_free_assigned=True,
                free_assign_date= DT.date.today() + DT.timedelta(days=7*6),
                free_assign_end_date= DT.date.today() + DT.timedelta(days=7*7)                
            )

        ]
    )  
  session.commit()

0 个答案:

没有答案