1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
from itertools import chain
import sqlalchemy as sa
from sqlalchemy_utils.functions import non_indexed_foreign_keys
from tests import TestCase
class TestFindNonIndexedForeignKeys(TestCase):
def create_models(self):
class User(self.Base):
__tablename__ = 'user'
id = sa.Column(sa.Integer, autoincrement=True, primary_key=True)
name = sa.Column(sa.Unicode(255))
class Category(self.Base):
__tablename__ = 'category'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Unicode(255))
class Article(self.Base):
__tablename__ = 'article'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Unicode(255))
author_id = sa.Column(
sa.Integer, sa.ForeignKey(User.id), index=True
)
category_id = sa.Column(sa.Integer, sa.ForeignKey(Category.id))
category = sa.orm.relationship(
Category,
primaryjoin=category_id == Category.id,
backref=sa.orm.backref(
'articles',
)
)
self.User = User
self.Category = Category
self.Article = Article
def test_finds_all_non_indexed_fks(self):
fks = non_indexed_foreign_keys(self.Base.metadata, self.engine)
assert (
'article' in
fks
)
column_names = list(chain(
*(
names for names in (
fk.columns.keys()
for fk in fks['article']
)
)
))
assert 'category_id' in column_names
assert 'author_id' not in column_names
|