Backport commit 5f2f47f of Django.
Backport commit 5f2f47f of Django.

This improves resiliency to unexpected errors.

Also use self.Database to make subclassing easier, like in check_constraints.

theoriginalgri/django-mssql,theoriginalgri/django-mssql