在SQLAlchemy中使用Postgres/PostGIS视图 [英] Work with Postgres/PostGIS View in SQLAlchemy

查看:127
本文介绍了在SQLAlchemy中使用Postgres/PostGIS视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个问题:

  • 我想在我的PostGIS-DB中生成一个View.如何将此视图添加到我的geometry_columns表中?

  • i want to generate a View in my PostGIS-DB. How do i add this View to my geometry_columns Table?

要与SQLAlchemy一起使用视图,我必须做什么? Table和View to SQLAlchemy之间有区别吗?还是可以使用与使用Table相同的方式来使用View?

What i have to do, to use a View with SQLAlchemy? Is there a difference between a Table and View to SQLAlchemy or could i use the same way to use a View as i do to use a Table?

对不起,我英语不好.

如果对我的问题有疑问,请随时提出,以便我可以尝试用另一种方式解释它:)

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

Nico

推荐答案

SQLAlchemy中的表对象具有两个作用.它们可用于发出DDL命令以在数据库中创建表.但是它们的主要目的是描述可以从中选择和插入的表格数据的列和类型.

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.

如果只想选择,则视图看起来像常规表一样看起来像SQLAlchemy.将视图描述为带有您感兴趣的列的表就足够了(您甚至不需要描述所有列).如果要使用ORM,则需要为SQLAlchemy声明可以将某些列组合用作主键(任何唯一的操作都可以).将某些列声明为外键也将使建立任何关系更加容易.如果您不为该Table对象发出create,那么SQLAlchemy只是知道如何查询数据库的元数据.

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.

如果您还想插入视图,则需要在视图上创建PostgreSQL规则或触发器,以将写入重定向到正确的位置.我不知道在Python端重定向写操作的好用法.

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.

这篇关于在SQLAlchemy中使用Postgres/PostGIS视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆