Debian Codegraph @

docs/legacy-quickstart.rst

raw
1
2 :orphan:
3
4 Legacy Quickstart
5 ======================
6
7 .. warning::
8 This guide shows you how to initialize the extension and define models
9 when using the SQLAlchemy 1.x style of ORM model classes. We encourage you to
10 upgrade to `SQLAlchemy 2.x`_ to take advantage of the new typed model classes.
11
12 .. _SQLAlchemy 2.x: https://docs.sqlalchemy.org/en/20/orm/quickstart.html
13
14 Initialize the Extension
15 ------------------------
16
17 First create the ``db`` object using the ``SQLAlchemy`` constructor.
18
19 When using the SQLAlchemy 1.x API, you do not need to pass any arguments to the ``SQLAlchemy`` constructor.
20 A declarative base class will be created behind the scenes for you.
21
22 .. code-block:: python
23
24 from flask import Flask
25 from flask_sqlalchemy import SQLAlchemy
26 from sqlalchemy.orm import DeclarativeBase
27
28 db = SQLAlchemy()
29
30
31 Using custom MetaData and naming conventions
32 --------------------------------------------
33
34 You can optionally construct the :class:`.SQLAlchemy` object with a custom
35 :class:`~sqlalchemy.schema.MetaData` object. This allows you to specify a custom
36 constraint `naming convention`_. This makes constraint names consistent and predictable,
37 useful when using migrations, as described by `Alembic`_.
38
39 .. code-block:: python
40
41 from sqlalchemy import MetaData
42 from flask_sqlalchemy import SQLAlchemy
43
44 db = SQLAlchemy(metadata=MetaData(naming_convention={
45 "ix": 'ix_%(column_0_label)s',
46 "uq": "uq_%(table_name)s_%(column_0_name)s",
47 "ck": "ck_%(table_name)s_%(constraint_name)s",
48 "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
49 "pk": "pk_%(table_name)s"
50 }))
51
52 .. _naming convention: https://docs.sqlalchemy.org/core/constraints.html#constraint-naming-conventions
53 .. _Alembic: https://alembic.sqlalchemy.org/en/latest/naming.html
54
55
56
57 Define Models
58 -------------
59
60 Subclass ``db.Model`` to define a model class. This is a SQLAlchemy declarative base
61 class, it will take ``Column`` attributes and create a table.
62
63 .. code-block:: python
64
65 class User(db.Model):
66 id = db.Column(db.Integer, primary_key=True)
67 username = db.Column(db.String, unique=True, nullable=False)
68 email = db.Column(db.String)
69
70 For convenience, the extension object provides access to names in the ``sqlalchemy`` and
71 ``sqlalchemy.orm`` modules. So you can use ``db.Column`` instead of importing and using
72 ``sqlalchemy.Column``, although the two are equivalent.
73
74 Unlike plain SQLAlchemy, Flask-SQLAlchemy's model will automatically generate a table name
75 if ``__tablename__`` is not set and a primary key column is defined.
76 The table name ``"user"`` will automatically be assigned to the model's table.
77
78
79 Create the Tables
80 -----------------
81
82 Defining a model does not create it in the database. Use :meth:`~.SQLAlchemy.create_all`
83 to create the models and tables after defining them. If you define models in submodules,
84 you must import them so that SQLAlchemy knows about them before calling ``create_all``.
85
86 .. code-block:: python
87
88 with app.app_context():
89 db.create_all()
90
91 Querying the Data
92 -----------------
93
94 You can query the data the same way regardless of SQLAlchemy version.
95 See :doc:`queries` for more information about queries.