Asmoox
Asmoox

Reputation: 652

Peewee retrieves data in python console but not in app

I have entities designed with peewee in Python. Before I started implementing real database, I've made several tests with in-memory databases. When I started to implement database functionality, I faced strange problem. My queries returns empty results, what more it depends if I run script or use python console.

First of all, let me proof that logic is correct. When I use python console, everything is ok:

>>> from Entities import *
>>> print (RouterSettings.select().where(RouterSettings.name=='RUT00').get().name)
RUT00

As you see, everything is correct. Specific query is executed and returns result. Now the same in a script:

from Entities import *
print (RouterSettings.select().where(RouterSettings.name=='RUT00').get().name)

This one returns exception instance matching query does not exist

print (RouterSettings.select().where(RouterSettings.name=='RUT00').get().name) File "C:\Users\Kamil\AppData\Local\Programs\Python\Python37-32\lib\site-packages\peewee.py", line 5975, in get (clone.model, sql, params)) Entities.RouterSettingsDoesNotExist: instance matching query does not exist : SQL: SELECT "t1"."id", "t1"."name", "t1"."ip", "t1"."username", "t1"."password", "t1"."model", "t1"."phone_num", "t1"."provider", "t1"."location" FROM "routersettings" AS "t1" WHERE ("t1"."name" = ?) LIMIT ? OFFSET ? Params: ['RUT00', 1, 0]

When I was trying to debug, I've found that database was as if not created: enter image description here Please note that within debugged variables database object is null (None). Do you have any ideas what's going on?

My Entities are defined as follows:

from peewee import *


class EnumField(IntegerField):

    def __init__(self, *argv):
        super().__init__()
        self.enum = []
        for label in argv:
            self.enum.append(label)

    def db_value(self, value):
        try:
            return self.enum.index(value)
        except ValueError:
            raise EnumField.EnumValueDoesnExistError(
                "Value doesn\'t exist in enum set.\nMaybe you forgot to add "
                "that one: " + value + "?")

    def python_value(self, value):
        try:
            return self.enum[value]
        except IndexError:
            raise EnumField.EnumValueDoesnExistError(
                'No value for given id')

    class EnumValueDoesnExistError(Exception):
        pass

class ModelField(EnumField):

    def __init__(self):
        super().__init__('RUT955_Q', 'RUT955_H', 'GLiNet300M')

class ProviderField(EnumField):

    def __init__(self):
        super().__init__('Orange', 'Play', 'Virgin')


class BaseModel(Model):
    class Meta:
        database = SqliteDatabase('SIMail.db', pragmas={'foreign_keys': 1})


class RouterSettings(BaseModel):

    name = CharField(unique=True)
    ip = CharField(unique=True)
    username = CharField()
    password = CharField()
    model = ModelField()
    phone_num = IntegerField(unique=True)
    provider = ProviderField()
    location = CharField()

Upvotes: 0

Views: 387

Answers (1)

coleifer
coleifer

Reputation: 26245

You probably are running it with a relative path to the database file, and depending on the current working directory when you're running your app vs the console, its using a different database file.

Upvotes: 2

Related Questions