Aryan Soni
Aryan Soni

Reputation: 266

SQLAlchemy Error Table not found

I am new to using sqlAlchemy and i keep getting this error when i try inserting a data entry. Please tell me what i am doing wrong.

test.py code:

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

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

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)

Upvotes: 0

Views: 6565

Answers (2)

Aryan Soni
Aryan Soni

Reputation: 266

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.

Upvotes: 4

user_D_A__
user_D_A__

Reputation: 460

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.pycode I got the following result when i did that

> 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

Upvotes: 0

Related Questions