Search code examples
pythonpostgresqlormponyorm

Postgres schemas with Pony ORM


How can I select which postgres schema to use in PonyORM?

I've tried to login with a role that has permission to only one schema called "test1", but it connects me to the public schema. So, i deleted the public schema an then it gave me an error:

 ProgrammingError: no schema has been selected to create in
 LINE 1: CREATE TABLE "customers" (

Solution

  • You can do it in two possible ways.

    First is specify your connection

    db = Database()
    ... # models definition
    pg = dict(
        provider='postgres', 
        user='username', 
        password='pwd', 
        host='localhost', 
        database='db', 
        options='-c search-path=SCHEMA NAME') # here you specify default schema
    db.bind(**pg)
    db.generate_mapping(create_tables=True)
    

    Second is specify _table_ option for entity

    class Person(db.Entity):
        _table_ = ('schemaname', 'tablename')
        attribute = ...