From a3ccd6cea1968899b6184c9d5d6e1b3d364a242c Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sat, 18 Dec 2021 01:48:12 +0100 Subject: [PATCH] feat(db): Use named database constraints --- flaschengeist/database.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/flaschengeist/database.py b/flaschengeist/database.py index 410e0cf..ebda993 100644 --- a/flaschengeist/database.py +++ b/flaschengeist/database.py @@ -1,9 +1,31 @@ from flask_sqlalchemy import SQLAlchemy +from sqlalchemy import MetaData -db = SQLAlchemy() +# https://alembic.sqlalchemy.org/en/latest/naming.html +metadata = MetaData( + naming_convention={ + "pk": "pk_%(table_name)s", + "ix": "ix_%(table_name)s_%(column_0_name)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", + } +) + + +db = SQLAlchemy(metadata=metadata) def case_sensitive(s): + """ + Compare string as case sensitive on the database + + Args: + s: string to compare + + Example: + User.query.filter(User.name == case_sensitive(some_string)) + """ if db.session.bind.dialect.name == "mysql": from sqlalchemy import func