【问题标题】:table X has no column named Y - SQLAlchemy表 X 没有名为 Y 的列 - SQLAlchemy
【发布时间】:2019-02-04 17:52:43
【问题描述】:

我在 python 中使用 SQLAlchemy 库来映射我的数据库。我创建了一个包含我的数据库的文件,它运行没有问题。但是当我创建一个指定我的值的新行时,我得到一个错误说

表 menu_item 没有名为 description 的列

 class MenuItem(Base):

        __tablename__ = 'menu_item'

        name = Column(String(80), nullable=False)
        id = Column(Integer, primary_key=True)
        course = Column(String(80))
        description = Column(String(250))
        price = Column(String(8))
        restaurant_id = Column(Integer, ForeignKey('restaurant.id'))
        restaurant = relationship(Restaurant)

这是我的课程 MenuItem 这是我的行值。

Cheesepizza = MenuItem(
    name="Chesse Pizza",
    description="Made with all natural ingredients and hot mozzarilla",
    course='Entree', price='$8.99', restaurant=myFirstRestaurant)

我希望我应该为每一行分配 5 个值。我错过了什么吗?

【问题讨论】:

  • 您是否在创建表格后修改了模型,添加了所述列?

标签: python sql sqlalchemy


【解决方案1】:

我想通了,我首先创建了一个没有描述列的数据库,但是当我添加它并再次运行 python 文件时,它没有更新文件以添加列(我不知道为什么)所以,我不得不删除 .db 文件并运行 python 文件来重新创建数据库并且它成功了。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
相关资源
最近更新 更多