我正在终端脚本的终端上进行操作,遵循有关声明A映射的本教程http://docs.sqlalchemy.org/en/latest/orm/tutorial.html SQLAlchemy教程。我需要输入

>>> from sqlalchemy import Column, Integer, String
>>> class User(Base):
        __tablename__ = 'users'
        id = Column(Integer, primary_key=True)
        name = Column(String)
        fullname = Column(String)
        password = Column(String)

        def __repr__(self):
            return "<\User(name='%s', fullname='%s', password='%s')>" % (
                self.name, self.fullname, self.password)


问题是在我输入密码= Column(String)之后,我按两次Enter键,然后....更改为>>>。然后,我重新输入所有内容,但由于该类已经存在而引发了错误……我不确定如何解决此问题。如何在Shell脚本中打开该类并进行编辑(在def repr中添加)

引发的错误如下:

/Users/GaryPeters/TFsqlAlc001/lib/python2.7/site-packages/sqlalchemy/ext/declarative/clsregistry.py:160: SAWarning: This declarative base already contains a class with the same class name and module name as __main__.User, and will be replaced in the string-lookup table.
  existing.add_item(cls)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

  File "/Users/GaryPeters/TFsqlAlc001/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py", line 53, in __init__
    _as_declarative(cls, classname, cls.__dict__)

  File "/Users/GaryPeters/TFsqlAlc001/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 251, in _as_declarative
    **table_kw)

  File "/Users/GaryPeters/TFsqlAlc001/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 339, in __new__
    "existing Table object." % key)

sqlalchemy.exc.InvalidRequestError: Table 'users' is already defined for this MetaData instance.  Specify 'extend_existing=True' to redefine options and columns on an existing Table object.

最佳答案

只需关闭然后重新打开外壳,然后再次键入所有内容,这一次确保仅按一次Enter键,而不要两次。

另外,请确保在遇到空白行时添加缩进-如果您按Enter键,然后按Tab键或间隔适当的时间,以便缩进正确的级别,那么您应该可以再次按Enter键没有外壳结束您的定义并再次显示>>>

您还应该在shell中重新定义该类,所以我不太确定“抛出错误”的含义-如果您要编辑帖子以包含特定的堆栈跟踪,这可能会有所帮助。

10-01 15:55