def post_migrate_mpathnode(model):
# Note: model *isn't* a subclass of MPathNode, because django migrations are Weird.
# if not issubclass(model, MPathNode):
# Hence the following workaround:
try:
ltree_field = model._meta.get_field('ltree')
if not isinstance(ltree_field, LTreeField):
return
except FieldDoesNotExist:
return
names = {
"table": quote_ident(model._meta.db_table, connection.connection),
"check_constraint": quote_ident('%s__check_ltree' % model._meta.db_table, connection.connection),
}
cur = connection.cursor()
# Check that the ltree is always consistent with being a child of _parent
cur.execute('''
ALTER TABLE %(table)s ADD CONSTRAINT %(check_constraint)s CHECK (
(parent_id IS NOT NULL AND ltree ~ (parent_id::text || '.*{1}')::lquery)
OR (parent_id IS NULL AND ltree ~ '*{1}'::lquery)
)
''' % names)
评论列表
文章目录