Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in SQLAlchemy base.metadata.create_all(engine) in python console not showing table?

I am trying to create database and insert values in it. I am using postgres database. I do not see any error when I do following

>>> from sqlalchemy import create_engine
>>> engine = create_engine('postgresql://postgres:password@localhost:5432/mydatabase')
>>> from sqlalchemy.ext.declarative import declarative_base
>>> Base = declarative_base()
>>> from sqlalchemy import Column, Integer, String
>>> class User(Base):
...     __tablename__ = 'users'
...     id = Column(Integer, primary_key=True)
...     fullname = Column(String)
...     password = Column(String)
...     
...     def __repr__(self):
...             return "<User(name='%s', fullname='%s', password='%s')>" % (self.name, self.fullname, self.password)


>>> User.__table__

gives me

Table('users', MetaData(bind=None), Column('id', Integer(), table=<users>, primary_key=True, nullable=False), Column('fullname', String(), table=<users>), Column('password', String(), table=<users>), schema=None)

But when I run

>>> Base.metadata.create_all(engine)

Nothing shows up, In document it is given that table and its attributes are shown But nothing is seen after running Base.metadata.create_all(engine)

Could some body suggest some solution ?

like image 703
Error_Coding Avatar asked May 16 '14 23:05

Error_Coding


1 Answers

If you want to see the commands SQLAlchemy is sending, you want to turn echo on in the engine:

engine = create_engine('postgresql://...', echo=True)

Of course, you could always just look at the database afterwards with psql or another tool.

like image 92
davidism Avatar answered Oct 02 '22 07:10

davidism