问题解决:django模型查询报错,找不到数据库表

django项目,使用的postgresql数据库,建了多个模式,模型查询时一直默认查public的表

1. 问题

django.db.utils.ProgrammingError: relation "ip_management_app.table" does not exist

2. 代码:

python 复制代码
class IpTag(models.Model):
    id = models.AutoField(primary_key=True)
    update_time = models.BigIntegerField()
    user_id = models.IntegerField()
    tab_type = models.IntegerField(default=1)
    tag = models.CharField(max_length=150)
    prefix_id = models.IntegerField()

    class Meta:
        managed = False
        db_table = 'ip_tag'


IpTag.objects.all()

3. 解决方案

(1)使用的django2.0,settings文件中修改数据库配置,增加OPTIONS

python 复制代码
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': '',
        'USER': '',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '5432',
        'OPTIONS': {
            'options': '-c search_path=ip_management_app,public'
        }
    }
}

(2)其他方案

python 复制代码
class IpTag(models.Model):
    id = models.AutoField(primary_key=True)
    update_time = models.BigIntegerField()
    user_id = models.IntegerField()
    tab_type = models.IntegerField(default=1)
    tag = models.CharField(max_length=150)
    prefix_id = models.IntegerField()

    class Meta:
        managed = False
        db_table = 'ip_management_app.ip_tag'


IpTag.objects.all()

修改db_table,有这样的解决方案,但是对我没有效果,不知是否django版本问题,有问题的可以都试下,第一种解决了我的问题

相关推荐
金銀銅鐵11 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup1116 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi0018 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵20 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf21 小时前
Agent 流程编排
后端·python·agent
copyer_xyf21 小时前
Agent RAG
后端·python·agent
copyer_xyf21 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf21 小时前
Agent 记忆管理
后端·python·agent
星云穿梭2 天前
用Python写一个带图形界面的学生管理系统——完整教程
python