Support for SQLite pragma and strict table settings #1247
Replies: 4 comments
-
We don't have a way of creating strict tables currently with Piccolo. Strict tables would be problematic for some of our column types (like If working from an existing database with strict tables it should work OK, as long as you keep to the narrower set of column types ( With PRAGMAs, we do currently set some: piccolo/piccolo/engine/sqlite.py Line 696 in 9517f49 We could extend this pretty easily to allow the user to specify their own PRAGMA statements. Presumably you would want to use this to enable strict mode.
Yes, exactly. It's just a problem with SQLite if you have multiple |
Beta Was this translation helpful? Give feedback.
-
Hmm. I guess
I'm guessing you could switch it out for TypesMy main concern is catching types before they get added to the SQLite database, as it's far from type safe. I had my model setup in Peewee ORM but the My follow-up questions would be:
PRAGMAThat'd be super helpful to add support. So far I've been using Async
So it seems like better to just use |
Beta Was this translation helpful? Give feedback.
-
Piccolo will not catch wrong types before inserting (we can insert Exampleimport asyncio
from piccolo.columns import Varchar, Array
from piccolo.table import Table, create_db_tables
from piccolo.utils.pydantic import create_pydantic_model
from piccolo.engine.sqlite import SQLiteEngine
from pydantic import BaseModel
DB = SQLiteEngine()
# Table example
class Manager(Table, db=DB):
name = Varchar()
arr_column = Array(base_column=Varchar())
# Models using Piccolo
ManagerModelIn = create_pydantic_model(table=Manager)
# Models using Pydantic
# class ManagerModelIn(BaseModel):
# name: str
# arr_column: list[str]
async def main():
# Table creating
await create_db_tables(Manager, if_not_exists=True)
# data = ManagerModelIn(name=1, arr_column=[1, 2])
# will raise ValidationError because we
# are using the wrong types (integer, not string)
data = ManagerModelIn(name="John", arr_column=["a", "b"]) # correct
manager = Manager(**data.model_dump())
await manager.save()
# select data
print(await Manager.select())
if __name__ == "__main__":
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
-
@dantownsend @sinisaos Has there been any movement on PRAGMAs? I prefer to keep my API layer models and Data layer models separate (this seems better practice to me?) as in this example so it seems your From a quick look at your docs it seems you're only using Pydantic on the API layer and not doing secondary checks before putting it into the database. In general (if you don't mess up your code) .. is this safe to do? I know enough about my coding skills (Elm has nice compiler errors, Python is rubbish) that I'm going to mess up and with SQLite that seems a risk 😄 — having that PRAGMA would act as an easier safeguard. I'm assuming by "write Pydantic models ourselves" you're meaning that |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Apologies if this is somewhere in the documentation, but I can't find it. I'm looking for a replacement for Peewee (which has pragma and
strict_tables
support) that's async with FastAPI. Do you support these settings?Also, a little confused about this SQLite async page. Is this only relevant if you're running a
select
then aninsert
/update
/delete
(in that order?) — for example, if you were to run an insert first, then a select, within the same function, you can just follow the regular docs?Beta Was this translation helpful? Give feedback.
All reactions