A flake8 plugin for SQLAlchemy code.
pip install flake8-sqlalchemy
By default, all checks are enabled.
You can disable all checks by adding the following to your setup.cfg
:
[flake8]
ignore = SQA
or ignore specific checks:
[flake8]
ignore = SQA100
Checks that when sqlalchemy
is imported with an alias,
the alias is either sa
or db
.
import sqlalchemy as foo
import sqlalchemy as sa
# or
import sqlalchemy as db
When writing a Column
definition the comment
keyword argument is required.
This provides inline documentation for the column,
as well as generating the SQL to add the comment to the database.
class Users(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String)
class Users(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, comment="User ID from Auth Service")
name = Column(String, comment="User name: first, middle, last")
Also applies to mapped_column
:
class Users(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, comment="User ID from Auth Service")
name = mapped_column(String, comment="User name: first, middle, last")
Encourages the use of back_populates
instead of backref
in SQLAlchemy relationships to ensure clarity and consistency in bidirectional relationships.
class Parent(Base):
__tablename__ = "parent"
id = Column(Integer, primary_key=True)
children = relationship("Child", backref="parent")
class Parent(Base):
__tablename__ = "parent"
id = Column(Integer, primary_key=True)
children = relationship("Child", back_populates="parent")
class Child(Base):
__tablename__ = "child"
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey("parent.id"))
parent = relationship("Parent", back_populates="children")
This project is licensed under the terms of the MIT license. See the LICENSE file for the full license text.