tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.66k stars 387 forks source link

cant fetch data from models #672

Open Sunda001 opened 3 years ago

Sunda001 commented 3 years ago

so am trying to fetch my data using .all() or .get() from models but it throws KeyError

here is my models:

class Koneksi(Model):
    id = fields.IntField(pk=True)
    chat_id = fields.BigIntField()

.all() traceback: https://nekobin.com/jifewejoca .get() traceback: https://nekobin.com/wuxetiwunu

long2ice commented 3 years ago

What's the full code?

Sunda001 commented 3 years ago

What's the full code?

its just: await Koneksi.create(chat_id=1) await Koneksi.all() or await Koneksi.get()

long2ice commented 3 years ago

So strange, you can debug to the earliest stack to see what happened

Sunda001 commented 3 years ago

here is the debug:

2021-03-16 21:45:38 - db_client:68 - DEBUG - Created connection <Connection(Thread-7, started 140392199079488)> with params: filename=queue.sqlite journal_mode=WAL journal_size_limit=16384 foreign_keys=ON
2021-03-16 21:45:38 - tortoise:30 - DEBUG - Creating schema: CREATE TABLE IF NOT EXISTS "koneksi" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    "chat_id" BIGINT NOT NULL
);
CREATE TABLE IF NOT EXISTS "storing" (
    "chat_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    "data" TEXT NOT NULL
);
2021-03-16 21:45:38 - db_client:147 - DEBUG - CREATE TABLE IF NOT EXISTS "koneksi" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    "chat_id" BIGINT NOT NULL
);
CREATE TABLE IF NOT EXISTS "koneksi2" (
    "ids" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    "chat_id" BIGINT NOT NULL
);
CREATE TABLE IF NOT EXISTS "storing" (
    "chat_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    "data" TEXT NOT NULL
);
2021-03-16 21:45:48 - db_client:132 - DEBUG - SELECT "chat_id","id" FROM "koneksi": None
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/.local/lib/python3.8/site-packages/tortoise/models.py in _init_from_db(cls, **kwargs)
    700             for key, model_field, field in meta.db_native_fields:
--> 701                 setattr(self, model_field, kwargs[key])
    702             # Fields that don't override .to_python_value() are converted without a call

KeyError: 'id'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-444-56b93ce015f3> in <module>
----> 1 await Koneksi.all()

~/.local/lib/python3.8/site-packages/tortoise/queryset.py in _execute(self)
    800 
    801     async def _execute(self) -> List[MODEL]:
--> 802         instance_list = await self._db.executor_class(
    803             model=self.model,
    804             db=self._db,

~/.local/lib/python3.8/site-packages/tortoise/backends/base/executor.py in execute_select(self, query, custom_fields)
    129                 keys = list(dict_row.keys())
    130                 values = list(dict_row.values())
--> 131                 instance: "Model" = self.model._init_from_db(
    132                     **dict(zip(keys[:current_idx], values[:current_idx]))
    133                 )

~/.local/lib/python3.8/site-packages/tortoise/models.py in _init_from_db(cls, **kwargs)
    712             # TODO: Apply similar perf optimisation as above for partial
    713             for key, value in kwargs.items():
--> 714                 setattr(self, key, meta.fields_map[key].to_python_value(value))
    715 
    716         return self

KeyError: '"id"'

but uh when i rename class of Koneksi to Koneksi2 its fixed

isaquealves commented 3 years ago

If changing the model's class name makes things work, maybe the issue could be related to the database itself. Did you try to drop those tables and create them again with aerich init-db?