我正在尝试创建数据库并在其中插入值。我正在使用postgres数据库。我跟随时没有看到任何错误
>>> 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__
给我
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)
但是当我跑步时
>>> Base.metadata.create_all(engine)
什么都没有显示,在文档中给出了表及其属性,但是运行Base.metadata.create_all(engine)后什么也看不到
有人可以提出解决办法吗?
如果要查看SQLAlchemy发送的命令,请echo
在引擎中打开:
engine = create_engine('postgresql://...', echo=True)
当然,您以后总是可以使用psql
或其他工具查看数据库。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句