Skip to content

Commit

Permalink
Merge pull request #129 from Pylons/sqla-2.0
Browse files Browse the repository at this point in the history
Convert sqlalchemy code to 2.0
  • Loading branch information
mmerickel authored Feb 4, 2024
2 parents b512391 + aa3faf1 commit 745088e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ unreleased

- Use ``pyproject.toml`` instead of ``setup.py`` for project metadata.

- Use SQLAlchemy 2.0 patterns.

2.0 (2021-02-28)
----------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ def my_view(request):

{%- elif cookiecutter.backend == 'sqlalchemy' %}
from pyramid.response import Response
from sqlalchemy.exc import SQLAlchemyError
import sqlalchemy as sa

from .. import models


@view_config(route_name='home', renderer='{{ cookiecutter.repo_name }}:templates/mytemplate.{{ "pt" if "chameleon" == cookiecutter.template_language else cookiecutter.template_language }}')
def my_view(request):
try:
query = request.dbsession.query(models.MyModel)
one = query.filter(models.MyModel.name == 'one').one()
except SQLAlchemyError:
query = sa.select(models.MyModel).where(models.MyModel.name == 'one')
one = request.dbsession.execute(query).scalar_one()
except sa.exc.SQLAlchemyError:
return Response(db_err_msg, content_type='text/plain', status=500)
return {'one': one, 'project': '{{ cookiecutter.project_name }}'}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from sqlalchemy.schema import MetaData

# Recommended naming convention used by Alembic, as various different database
Expand Down

0 comments on commit 745088e

Please sign in to comment.