Nico
Nico

Reputation: 103

Work with Postgres/PostGIS View in SQLAlchemy

Two questions:

sorry for my poor english.

If there a questions about my question, please feel free to ask so i can try to explain it in another way maybe :)

Nico

Upvotes: 2

Views: 1973

Answers (1)

Ants Aasma
Ants Aasma

Reputation: 54882

Table objects in SQLAlchemy have two roles. They can be used to issue DDL commands to create the table in the database. But their main purpose is to describe the columns and types of tabular data that can be selected from and inserted to.

If you only want to select, then a view looks to SQLAlchemy exactly like a regular table. It's enough to describe the view as a Table with the columns that interest you (you don't even need to describe all of the columns). If you want to use the ORM you'll need to declare for SQLAlchemy that some combination of the columns can be used as the primary key (anything that's unique will do). Declaring some columns as foreign keys will also make it easier to set up any relations. If you don't issue create for that Table object, then it is just metadata for SQLAlchemy to know how to query the database.

If you also want to insert to the view, then you'll need to create PostgreSQL rules or triggers on the view that redirect the writes to the correct location. I'm not aware of a good usage recipe to redirect writes on the Python side.

Upvotes: 4

Related Questions