Roman  Kliuchko
Roman Kliuchko

Reputation: 499

How to specify schema name in SQLAlchemy insert statement?

Let's say I'm writing simple script that will insert some data to my DB with SQLAlchemy low-level API (here I don't need models and all the stuff). Here is a code:

engine = create_engine(os.getenv('DB_URL')
conn = engine.connect()

insert_stmt = insert(
    table(
        'my_events',
        column('my_column')
    ),
    values={
        'my_column': 'val1'
    }
)
conn.execute(insert_stmt)

The question here is how can I specify schema name where the table resides?

Upvotes: 0

Views: 2413

Answers (1)

Mathieu Roger
Mathieu Roger

Reputation: 76

You can use the schema parameter in Table constructor, see:

https://docs.sqlalchemy.org/en/13/core/metadata.html#sqlalchemy.schema.Table.params.schema

insert_stmt = insert(
Table(
    'my_events',
    Column('my_column'),
    schema='my_schema'
),
values={
    'my_column': 'val1'
}
)

You can also use Metadata to tell what is the default schema, see:

Upvotes: 2

Related Questions