|
| 1 | +# Views |
| 2 | + |
| 3 | +Views definition and registration can be performed exactly as it is done with other object |
| 4 | +types, by defining the set of views on the `MetaData` or declarative base, like so: |
| 5 | + |
| 6 | +```python |
| 7 | +from sqlalchemy.ext.declarative import declarative_base |
| 8 | +from sqlalchemy_declarative_extensions import declarative_database, View, Views |
| 9 | + |
| 10 | +_Base = declarative_base() |
| 11 | + |
| 12 | + |
| 13 | +@declarative_database() |
| 14 | +class Base(_Base): |
| 15 | + __abstract__ = True |
| 16 | + |
| 17 | + views = Views().are( |
| 18 | + View("foo", "select * from bar where id > 10", schema="baz"), |
| 19 | + ) |
| 20 | +``` |
| 21 | + |
| 22 | +And if you want to define views using raw strings, or otherwise not reference the tables |
| 23 | +produced off the `MetaData`, then this is absolutely a valid way to organize. |
| 24 | + |
| 25 | +## The `view` decorator |
| 26 | + |
| 27 | +However views differ from most of the other object types, in that they are convenient to |
| 28 | +define **in terms of** the tables they reference (i.e. your existing set of models/tables). |
| 29 | +In fact personally, all of my views are produced from [select](sqlalchemy.sql.expression.select) expressions |
| 30 | +referencing the underlying [Table](sqlalchemy.schema.Table) object. |
| 31 | + |
| 32 | +This commonly introduce a circular reference problem wherein your tables/models are defined |
| 33 | +through subclassing the declarative base, which means your declarative base cannot then |
| 34 | +have the views statically defined **on** the base (while simultaneously referencing those models). |
| 35 | + |
| 36 | +```{note} |
| 37 | +There are ways of working around this in SQLAlchemy-land. For example by creating a ``MetaData`` |
| 38 | +ahead of time and defining all models in terms of their underlying ``Table``. |
| 39 | +
|
| 40 | +Or perhaps by using SQLAlchemy's mapper apis such that you're not subclassing the declarative base |
| 41 | +for models. |
| 42 | +
|
| 43 | +In any case, these options are more complex and probably atypical. As such, we cannot assume |
| 44 | +you will adopt them. |
| 45 | +``` |
| 46 | + |
| 47 | +For everyone else, the [view](sqlalchemy_declarative_extensions.view) decorator is meant to be the |
| 48 | +solution to that problem. |
| 49 | + |
| 50 | +This strategy allows one to organize their views alongside the models/tables those |
| 51 | +views happen to be referencing, without requiring the view be importable at MetaData/model base |
| 52 | +definition time. |
| 53 | + |
| 54 | +### Option 1 |
| 55 | + |
| 56 | +```python |
| 57 | +from sqlalchemy import Column, types, select |
| 58 | +from sqlalchemy.ext.declarative import declarative_base |
| 59 | +from sqlalchemy_declarative_extensions import view |
| 60 | + |
| 61 | +Base = declarative_base() |
| 62 | + |
| 63 | + |
| 64 | +class Foo(Base): |
| 65 | + __tablename__ = 'foo' |
| 66 | + |
| 67 | + id = Column(types.Integer, primary_key=True) |
| 68 | + |
| 69 | + |
| 70 | +@view() |
| 71 | +class Bar1(Base): |
| 72 | + __tablename__ = 'bar' |
| 73 | + __view__ = select(Foo.__table__).where(Foo.__table__.id > 10) |
| 74 | + |
| 75 | + id = Column(types.Integer, primary_key=True) |
| 76 | +``` |
| 77 | + |
| 78 | +The primary difference between Options 1 and 2 in the above example is of how the |
| 79 | +resulting classes are seen by SQLAlchemy/Alembic natively. |
| 80 | + |
| 81 | +In the case of `Bar1`, SQLAlchemy/Alembic actually think that class is a normal table. |
| 82 | +Therefore querying the view looks identical to a real table: `session.query(Bar1).all()` |
| 83 | + |
| 84 | +For alembic, this means that alembic thinks you defined a table and will attempt to |
| 85 | +autogenerate it (while this library will also notice it and attempt to autogenerate |
| 86 | +a conflicting view. |
| 87 | + |
| 88 | +In order to use this option, we suggest you use one or both of some utility functions provided |
| 89 | +under the `sqlalchemy_declarative_extensions.alembic`: [ignore_view_tables](sqlalchemy_declarative_extensions.alembic.ignore_view_tables) |
| 90 | +and [compose_include_object_callbacks](sqlalchemy_declarative_extensions.alembic.compose_include_object_callbacks). |
| 91 | + |
| 92 | +Somewhere in your Alembic `env.py`, you will have a block which looks like this: |
| 93 | + |
| 94 | +```python |
| 95 | +with connectable.connect() as connection: |
| 96 | + context.configure( |
| 97 | + connection=connection, |
| 98 | + target_metadata=target_metadata, |
| 99 | + ... |
| 100 | + ) |
| 101 | +``` |
| 102 | + |
| 103 | +The above call to `configure` accepts an `include_object`, which tells alembic to include or ignore |
| 104 | +all detected objects. |
| 105 | + |
| 106 | +```python |
| 107 | +from sqlalchemy_declarative_extensions.alembic import ignore_view_tables |
| 108 | +... |
| 109 | +context.configure(..., include_object=ignore_view_tables) |
| 110 | +``` |
| 111 | + |
| 112 | +If you happen to already be using `include_object` to perform filtering, we provide an additional |
| 113 | +utility to more easily compose our version with your own. Although you can certainly manually call |
| 114 | +`ignore_view_tables` directly, yourself. |
| 115 | + |
| 116 | +```python |
| 117 | +from sqlalchemy_declarative_extensions.alembic import ignore_view_tables, compose_include_object_callbacks |
| 118 | +... |
| 119 | +def my_include_object(object, *_): |
| 120 | + if object.name != 'foo': |
| 121 | + return True |
| 122 | + return False |
| 123 | + |
| 124 | +context.configure(..., include_object=compose_include_object_callbacks(my_include_object, ignore_view_tables)) |
| 125 | +``` |
| 126 | + |
| 127 | +## Option 2 |
| 128 | + |
| 129 | +```python |
| 130 | +from sqlalchemy import Column, types, select |
| 131 | +from sqlalchemy.ext.declarative import declarative_base |
| 132 | +from sqlalchemy_declarative_extensions import view |
| 133 | + |
| 134 | +Base = declarative_base() |
| 135 | + |
| 136 | + |
| 137 | +class Foo(Base): |
| 138 | + __tablename__ = 'foo' |
| 139 | + |
| 140 | + id = Column(types.Integer, primary_key=True) |
| 141 | + |
| 142 | + |
| 143 | +@view(Base) # or `@view(Base.metadata)` |
| 144 | +class Bar2: |
| 145 | + __tablename__ = 'bar' |
| 146 | + __view__ = select(Foo.__table__).where(Foo.__table__.id > 10) |
| 147 | +``` |
| 148 | + |
| 149 | +By contrast, with Option 2, your class is not subclassing `Base`, therefore it's |
| 150 | +not registered as a real table by SQLAlchemy or Alembic. There's no additional |
| 151 | +work required to get them to ignore the table, because it's not one. |
| 152 | + |
| 153 | +Unfortunately, that means you cannot **invisibly** treat it as though it's a normal model, |
| 154 | +largely because it doesn't have the columns enumerated out in the same way. |
| 155 | + |
| 156 | +However we can provide some basic support for treating it as a table as far as the ORM is concerned. |
| 157 | +For example, you can still `session.query(Bar2).all()` directly. |
| 158 | + |
| 159 | +However, in most cases views primarily benefit non-code consumers of the database, because there's |
| 160 | +no practical difference between querying a literal view, versus executing the underlying query |
| 161 | +of that view, through something like `session.execute(Bar2.__view__)`. |
0 commit comments