简体   繁体   English

找不到SQLAlchemy错误表

[英]SQLAlchemy Error Table not found

I am new to using sqlAlchemy and i keep getting this error when i try inserting a data entry. 我是使用sqlAlchemy的新手,尝试插入数据条目时始终收到此错误。 Please tell me what i am doing wrong. 请告诉我我在做什么错。

test.py code: test.py代码:

import sys
import sqlalchemy
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column,ForeignKey,Integer,String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine


engine = create_engine('sqlite:///restaurantmenu.db')
Base = declarative_base()


class Restaurant(Base):
   __tablename__ = "restaurant"
    name = Column(String(80),nullable=False)
    id = Column(Integer,primary_key=True)


class MenuItem(Base):
    __tablename__ = 'menu_item'
    name = Column(String(80),nullable=False)
    id = Column(Integer,primary_key = True)
    course = Column(String(250))
    description = Column(String(250))
    price = Column(String(9))
    restaurant_id = Column(Integer,ForeignKey('restaurant.id'))
    restaurant = relationship(Restaurant)
Base.metadata.create_all(engine)

accessDatabase.py code accessDatabase.py代码

import sys
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from test import Base,Restaurant,MenuItem

engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine,autoflush=False)
session = DBSession()
MyFirstRestaurant = Restaurant(name = "Pizza Palace")
session.add(MyFirstRestaurant)
session.commit()
session.query(Restaurant).all()

the problem occurs when I run accessDatabase.py and at the line session.commit 当我运行accessDatabase.py并在session.commit行时会发生问题

error here: 错误在这里:

   sqlalchemy.exc.OperationalError: 
    (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block 
if this flush is occurring prematurely) 
(sqlite3.OperationalError) no such table: restaurant [SQL: 'INSERT INTO restaurant (name) VALUES (?)'] [parameters: ('Pizza Palace',)] 
(Background on this error at: http://sqlalche.me/e/e3q8)

It's all a stupid mistake. 这都是一个愚蠢的错误。 Initializing the engine before creating table was the culprit. 罪魁祸首是在创建表之前初始化引擎。 After I shifted those two lines of code at the bottom of the file. 将文件底部的这两行代码转移后。 It ran finally. 终于跑了。 Engine and setting metadata should be done after creating the tables. 创建表后,应完成引擎和设置元数据。

Please, run the test.py code once to create the database schema with all the tables and the metadata and then proceed to run the accessDatabase.py code I got the following result when i did that 请运行一次test.py代码以创建包含所有表和元数据的数据库架构,然后继续运行accessDatabase.py代码,我这样做时得到以下结果

> C:\Users\userX\PycharmProjects\PegabaseFilter\venv\Scripts\python.exe
> C:/Users/userX/PycharmProjects/PegabaseFilter/test_2.py
> [<test.Restaurant object at 0x00000000032A8F28>, <test.Restaurant
> object at 0x00000000032A8A90>, <test.Restaurant object at
> 0x00000000032DF048>, <test.Restaurant object at 0x00000000032DF0B8>,
> <test.Restaurant object at 0x00000000032DF160>, <test.Restaurant
> object at 0x00000000032DF208>, <test.Restaurant object at
> 0x0000000003251BA8>] {'_sa_instance_state':
> <sqlalchemy.orm.state.InstanceState object at 0x00000000032A8FD0>,
> 'name': u'Pizza Palace', 'id': 1} {'_sa_instance_state':
> <sqlalchemy.orm.state.InstanceState object at 0x00000000032A8A90>,
> 'name': u'Pizza Palace', 'id': 2} {'_sa_instance_state':
> <sqlalchemy.orm.state.InstanceState object at 0x00000000032A8C88>,
> 'name': u'Pizza Palace', 'id': 3} {'_sa_instance_state':
> <sqlalchemy.orm.state.InstanceState object at 0x00000000032A85C0>,
> 'name': u'Pizza Palace_userX', 'id': 4} {'_sa_instance_state':
> <sqlalchemy.orm.state.InstanceState object at 0x00000000032A8588>,
> 'name': u'Pizza Palace_userX', 'id': 5} {'_sa_instance_state':
> <sqlalchemy.orm.state.InstanceState object at 0x00000000032A84A8>,
> 'name': u'Pizza Palace_userX', 'id': 6} {'_sa_instance_state':
> <sqlalchemy.orm.state.InstanceState object at 0x0000000003251C18>,
> 'name': u'Pizza Palace_userX', 'id': 7}

Base.metadata.create_all(engine) this will create not only the schema but all the objects , relations , attributes Base.metadata.create_all(engine)这将不仅创建模式,还创建所有对象,关系,属性

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

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