read-only peewee w/ Flask + PostgreSQL

195 views
Skip to first unread message

Jerry S.

unread,
Aug 25, 2019, 11:53:18 PM8/25/19
to peewee-orm
Hi, a couple questions for a new peewee scenario (using Flask and Postgres):

• Is it possible to tell peewee the db is read-only? This particular app will have no write or update access, I don't need peewee to create any tables, and there's no need for the possibility of INSERTs, UPDATEs, DELETEs, etc.  (there is another question in this mailing list about read-only but it refers to a local SQLite db)

• I'd like to use a SELECT query as the source of a model. I could define a view in the DB itself but I would prefer to write that query in the app (source control, fewer db objects to manage, one location if I want to make changes in the future). Possible?

• Following the Flask application factory example, is this line all I need to get a db connection with the PG extensions (like jsonb and timestamp+tz support) AND connection pool support?

  app.config['DATABASE'] = 'postgresext+pool://user:pw@server:5432/db'

• How can I define a connection that specifies a default schema within the db? I have a schema that is exclusively for this app, no other app will use this schema and this app has no reason to query other schemas. SQLAlchemy has a MetaData function to choose a schema, is there something similar in peewee?

Thanks very much for helping me get started, I think peewee is an attractive alternative to SQL-A.

Charles Leifer

unread,
Aug 26, 2019, 7:57:56 AM8/26/19
to peewe...@googlegroups.com
No, you can't tell peewee the db is read-only. Python is so dynamic, it'd be difficult. You need to just control your usage of the APIs and limit yourself to .select() calls.

It's tricky to write CREATE TABLE ... AS SELECT with certain db drivers because of limitations on the way parameters are interpolated. Just add a step to your schema creation (e.g. where you would call db.create_tables(...)):

db.execute_sql('create table "my_table" as select ...')

You need to then define a flask database wrapper:

app.config['DATABASE'] = 'postgresext+pool://...'
flask_db = FlaskDB(app)

class BaseModel(flask_db.Model):
    class Meta:
        schema = 'my_schema'  # Inherit BaseModel and you will use the given schema.

--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/peewee-orm/c372066b-2761-4803-8443-467f2f280e67%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages