peewee.OperationalError: no such table

简介: peewee.OperationalError: no such table

image.png

目录

问题描述

初始化数据表的时候,依次执行了

  • 建表
  • 查询数据

报错如下

peewee.OperationalError: no such table: tb_user

问题复现

测试环境

$ python --version
Python 3.7.0
$ pip show peewee
Name: peewee
Version: 3.15.3

定义Model

# -*- coding: utf-8 -*-
from peewee import TextField, AutoField, Model
from playhouse.sqliteq import SqliteQueueDatabase
db = SqliteQueueDatabase(database='database.db')
class UserModel(Model):
    id = AutoField()
    name = TextField()
    class Meta:
        database = db
        table_name = 'tb_user'

1、执行建表和插入

执行正常,没有报错

# 建表
db.create_tables([UserModel])
# 插入数据
UserModel.create(id=1, name='Tom')

2、执行建表和查询

  • 第一次执行的时候,都会报错,提示表不存在
  • 第二次之后执行,就不会报错
# 建表
db.create_tables([UserModel])
# 查询数据
row = UserModel.select('id').where(
    UserModel.id, '=', 1
).get_or_none()
print(row)
# peewee.OperationalError: no such table: tb_user

问题分析

文档:

原文:

SqliteQueueDatabase is designed to simplify things by sending all write queries through a single, long-lived connection. The benefit is that you get the appearance of multiple threads writing to the database without conflicts or timeouts. The downside, however, is that you cannot issue write transactions that encompass multiple queries – all writes run in autocommit mode, essentially.

SqliteQueueDatabase 是单线程写入和多线程读取,写的线程和读的线程并不在一起。

所以,写入还没完成就执行了读取操作,触发了表不存在的报错

问题解决

找到了问题所在,那我们等一等写入线程,再执行读操作

import time
time.sleep(0.01)

完整代码

# -*- coding: utf-8 -*-
import time
from peewee import TextField, AutoField, Model
from playhouse.sqliteq import SqliteQueueDatabase
db = SqliteQueueDatabase(database='database.db')
class UserModel(Model):
    id = AutoField()
    name = TextField()
    class Meta:
        database = db
        table_name = 'tb_user'
# 建表
db.create_tables([UserModel])
# 等一等,写入操作完成再读取
time.sleep(0.01)
# 查询数据
row = UserModel.select('id').where(
    UserModel.id, '=', 1
).get_or_none()
print(row)
# None

参考:Q: How to defer SqliteQueueDatabase? #2095


相关文章
|
机器学习/深度学习 分布式计算 大数据
什么是 Databricks?它的主要功能是什么?
【8月更文挑战第12天】
12021 7
|
设计模式 开发者 Python
Python中循环依赖问题及其解决方案
循环依赖是 Python 开发中需要特别注意的问题。通过重新设计模块结构、延迟导入、依赖注入、利用 Python 的动态特性以及代码重构等方法,可以有效地解决循环依赖问题。这些策略不仅有助于提高代码的可维护性和可读性,还能避免潜在的运行时错误。在实际开发中,开发者应该根据具体情况选择合适的解决方案。
|
JSON 前端开发 Java
spring data elasticsearch:从零搭建springboot整合spring data elasticsearch3.x环境
spring-data系列为众多中间件、数据库的操作提供了极其方便的API,对于elasticsearch也不例外,spring-data-elasticsearch不仅为我们提供了现成的CRUD接口,也提供了简便的各类java client的整合方案 所以今天我们就来讲解,如果在springboot项目中整合spring-data-elasticsearch
2423 0
spring data elasticsearch:从零搭建springboot整合spring data elasticsearch3.x环境
|
云安全 安全 网络安全
|
15天前
|
JSON 程序员 数据格式
最新版Notepad++下载、安装、配置保姆级教程(附官网安装包)
Notepad++ 是一款免费开源、轻量快速的 Windows 文本编辑器,支持多标签、语法高亮、代码折叠与宏录制,体积小、启动快、资源占用低,兼容 x64/x32/ARM 架构,是程序员和文字工作者的高效利器。
3699 0
|
8天前
|
JavaScript 安全 前端开发
Nodejs Brotli decoder 代码执行漏洞 (CVE-2026-111497)10629
Node.js发布安全更新修复Brotli解码器高危RCE漏洞(CVE-2026-111497),建议受影响版本用户立即升级。
2049 2
|
14天前
|
人工智能 API 开发工具
DeepSeek Harness 神插件:白嫖 13 家大厂模型
`dsh-our-free-model` 是 DeepSeek Harness 神级插件:零配置、零API Key、零账号,聚合13家大厂免费模型(含DeepSeek V4.1 Flash、Kimi K3等),支持本地网关、用量看板与自动降级,真正实现“白嫖即用”。
2507 46
|
11天前
|
Web App开发 人工智能 前端开发
2026 年 AI 浏览器怎么选?Ego Lite、Tabbit、Chrome DevTools MCP 深度对比,开发者到底该用哪个?
2026年AI浏览器选型指南:Ego Lite专注多Agent协同(Mac优先),Tabbit国产首选,原生支持Windows与Codex控制;Chrome DevTools MCP非浏览器而是开发者利器,深度集成Console/Network/Performance调试能力,让AI真正“看懂并验证”前端代码。
1479 3

热门文章

最新文章