python - (sqlite3.OperationalError) 没有这样的表
问题描述
我看到在我的目录中创建的 db 文件,但我无法插入其中。我的问题很基本,有人可以发现明显的吗?
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
import models
DB_PATH = 'sqlite:////home/ali/test.db'
engine = create_engine(DB_PATH, echo=True)
Session = sessionmaker(bind=engine)
session = Session()
### Insert data into ProductionRun table ###
prod_run = models.ProductionRun(instance_id=df.instance_id.iloc[0], name=df.profile_name.iloc[0], host=df.host_box.iloc[0], default_config={})
session.add(prod_run)
session.commit()
这是我看到的错误
OperationalError: (sqlite3.OperationalError) no such table: ProductionRun
[SQL: INSERT INTO "ProductionRun" (instance_id, name, host, default_config) VALUES (?, ?, ?, ?)]
[parameters: (1, 'Apple', 'host1', '{}')]
(Background on this error at: http://sqlalche.me/e/13/e3q8)
这里是 ProductionRunmodels.py
class ProductionRun(Base):
__tablename__ = 'ProductionRun'
id = Column(Integer, primary_key=True, autoincrement=True)
instance_id = Column(Integer, nullable=True)
name = Column(String, nullable=False)
host = Column(String, nullable=False)
default_config = Column(JSON, nullable=False)
status_change = relationship('ProductionRunStatusChange', back_populates='production_run')
解决方案
因为是 Sqlite,所以表类需要在创建数据库之前发生。
DB_PATH = 'sqlite:////home/ali/test.db'
engine = create_engine(DB_PATH, echo=True)
BASE.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
其他堆栈溢出答案的参考
- sqlalchemy.exc.OperationalError:(sqlite3.OperationalError)没有这样的表:项目
- 尝试添加项目时获取空的sqlite DB和“(sqlite3.OperationalError)没有这样的表:..:”
文档
推荐阅读
- c++ - 为什么sqlite的delete总是成功,即使要删除的行不存在?
- javascript - OpenVidu - 为现有发布者添加流 - Javascript
- javascript - Unable to check the multi-select checkbox filter of the Kendo Grid Programmatically
- java - findAll() 在 SpringBoot Rest MySql CRUD 操作应用程序中不起作用
- c++ - 通过 Wininet 的 HTTP POST 请求
- html - 为什么不同视口中的浏览器一直隐藏我只想隐藏某些视口大小的内容?
- javascript - 为什么我不能只声明脚本?
- php - 如何在 PHP 中获取“列 + 7 天”>= NOW 的位置
- python - 熊猫df中每15分钟一次的分组值
- java - 如何使用杰克逊从 JsonNode 对象中检索 JSON 元素作为字符串?