-
-
Notifications
You must be signed in to change notification settings - Fork 900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support the SQLAlchemy 2.0 DeclarativeBase models #1215
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
0150152
Possible way to mixin with DeclarativeBase
pamelafox 7d0f5e5
Remove pdm-python file
pamelafox d073fa6
Try different approach to mixins for 2.0
pamelafox 5ff15bc
Support MappedAsDataClass
pamelafox 8d78e37
Update should check
pamelafox 41fdd5d
Parameterizing all the tests
pamelafox 31e8e2c
Remove redundant test
pamelafox b536d28
Updates to docs
pamelafox af5096d
Getting types to work (mostly)
pamelafox a8da619
Update sqlalchemy version
pamelafox d3a30c5
Mark the type classes as internal with an underscore in front
pamelafox 73e0206
Fix tests
pamelafox 2c3650a
Addressing docs note
pamelafox 7610ea7
Add constraint of les than 2.1
pamelafox 8fdc47a
Remove version cap per Davids comment
pamelafox 1bbd722
Add disable_autonaming
pamelafox b052dd6
Update versionchanged for new mixins
pamelafox d8c31ca
Document refactor to emphasize 2.x, fix for bind key and metadata for…
pamelafox a89485b
Parameterize more tests for commit
pamelafox 6f4c180
Parameterize more tests to use 2.x
pamelafox caee46a
Update README
pamelafox 6d7a7e3
Style and sphinx checks
pamelafox 1d877e8
Documentation updates- remove new mixins from ref, add some comments
pamelafox a2db216
Update abstract models and mixins
pamelafox cf4170a
Add test for declared_attr
pamelafox fe9e4de
Make suggested typing fix
pamelafox fdeec1d
Adjusted to work in 3.8
pamelafox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
|
||
:orphan: | ||
|
||
Legacy Quickstart | ||
====================== | ||
|
||
.. warning:: | ||
This guide shows you how to initialize the extension and define models | ||
when using the SQLAlchemy 1.x style of ORM model classes. We encourage you to | ||
upgrade to `SQLAlchemy 2.x`_ to take advantage of the new typed model classes. | ||
|
||
.. _SQLAlchemy 2.x: https://docs.sqlalchemy.org/en/20/orm/quickstart.html | ||
|
||
Initialize the Extension | ||
------------------------ | ||
|
||
First create the ``db`` object using the ``SQLAlchemy`` constructor. | ||
|
||
When using the SQLAlchemy 1.x API, you do not need to pass any arguments to the ``SQLAlchemy`` constructor. | ||
A declarative base class will be created behind the scenes for you. | ||
|
||
.. code-block:: python | ||
|
||
from flask import Flask | ||
from flask_sqlalchemy import SQLAlchemy | ||
from sqlalchemy.orm import DeclarativeBase | ||
|
||
db = SQLAlchemy() | ||
|
||
|
||
Using custom MetaData and naming conventions | ||
-------------------------------------------- | ||
|
||
You can optionally construct the :class:`.SQLAlchemy` object with a custom | ||
:class:`~sqlalchemy.schema.MetaData` object. This allows you to specify a custom | ||
constraint `naming convention`_. This makes constraint names consistent and predictable, | ||
useful when using migrations, as described by `Alembic`_. | ||
|
||
.. code-block:: python | ||
|
||
from sqlalchemy import MetaData | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
db = SQLAlchemy(metadata=MetaData(naming_convention={ | ||
"ix": 'ix_%(column_0_label)s', | ||
"uq": "uq_%(table_name)s_%(column_0_name)s", | ||
"ck": "ck_%(table_name)s_%(constraint_name)s", | ||
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", | ||
"pk": "pk_%(table_name)s" | ||
})) | ||
|
||
.. _naming convention: https://docs.sqlalchemy.org/core/constraints.html#constraint-naming-conventions | ||
.. _Alembic: https://alembic.sqlalchemy.org/en/latest/naming.html | ||
|
||
|
||
|
||
Define Models | ||
------------- | ||
|
||
Subclass ``db.Model`` to define a model class. This is a SQLAlchemy declarative base | ||
class, it will take ``Column`` attributes and create a table. | ||
|
||
.. code-block:: python | ||
|
||
class User(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) | ||
username = db.Column(db.String, unique=True, nullable=False) | ||
email = db.Column(db.String) | ||
|
||
For convenience, the extension object provides access to names in the ``sqlalchemy`` and | ||
``sqlalchemy.orm`` modules. So you can use ``db.Column`` instead of importing and using | ||
``sqlalchemy.Column``, although the two are equivalent. | ||
|
||
Unlike plain SQLAlchemy, Flask-SQLAlchemy's model will automatically generate a table name | ||
if ``__tablename__`` is not set and a primary key column is defined. | ||
The table name ``"user"`` will automatically be assigned to the model's table. | ||
|
||
|
||
Create the Tables | ||
----------------- | ||
|
||
Defining a model does not create it in the database. Use :meth:`~.SQLAlchemy.create_all` | ||
to create the models and tables after defining them. If you define models in submodules, | ||
you must import them so that SQLAlchemy knows about them before calling ``create_all``. | ||
|
||
.. code-block:: python | ||
|
||
with app.app_context(): | ||
db.create_all() | ||
|
||
Querying the Data | ||
----------------- | ||
|
||
You can query the data the same way regardless of SQLAlchemy version. | ||
See :doc:`queries` for more information about queries. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps a radical approach, but I've deleted the Model Metaclass section of the customizing doc, since: