File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -468,8 +468,13 @@ def _create_collection(self, model):
468468 auto_encryption_opts = getattr (client ._options , "auto_encryption_opts" , None )
469469 if not auto_encryption_opts :
470470 raise ImproperlyConfigured (
471- f"Encrypted fields found but DATABASES['{ self .connection .alias } ']['OPTIONS'] "
472- "is missing auto_encryption_opts."
471+ f"Tried to create model { model ._meta .label } in "
472+ f"'{ self .connection .alias } ' database. The model has "
473+ "encrypted fields but "
474+ f"DATABASES['{ self .connection .alias } ']['OPTIONS'] is "
475+ 'missing the "auto_encryption_opts" parameter. If the '
476+ "model should not be created in this database, adjust "
477+ "your database routers."
473478 )
474479 encrypted_fields = self ._get_encrypted_fields (model )
475480 db .create_collection (db_table , encryptedFields = encrypted_fields )
Original file line number Diff line number Diff line change 11from bson .binary import Binary
2+ from django .core .exceptions import ImproperlyConfigured
23from django .db import connections
34
45from . import models
@@ -134,3 +135,18 @@ def test_key_creation_and_lookup(self):
134135 key = EncryptionKey .objects .get (key_alt_name = test_key_alt_name )
135136 self .assertEqual (key .id , field_info ["keyId" ])
136137 self .assertEqual (key .key_alt_name , [test_key_alt_name ])
138+
139+ def test_missing_auto_encryption_opts (self ):
140+ connection = connections ["default" ]
141+ msg = (
142+ "Tried to create model encryption_.Patient in 'default' database. "
143+ "The model has encrypted fields but DATABASES['default']['OPTIONS'] "
144+ 'is missing the "auto_encryption_opts" parameter. If the model '
145+ "should not be created in this database, adjust your database "
146+ "routers."
147+ )
148+ with (
149+ self .assertRaisesMessage (ImproperlyConfigured , msg ),
150+ connection .schema_editor () as editor ,
151+ ):
152+ editor .create_model (models .Patient )
You can’t perform that action at this time.
0 commit comments