MCPcopy
hub / github.com/tortoise/tortoise-orm / db

Method db

tortoise/context.py:438–468  ·  view source on GitHub ↗

Get a database connection by name. Args: connection_name: The connection alias. If None, uses the default connection. With a single connection, it becomes the default automatically. With multiple connections, eithe

(self, connection_name: str | None = None)

Source from the content-addressed store, hash-verified

436 return self._apps.get_model(app_label, model_name)
437
438 def db(self, connection_name: str | None = None) -> BaseDBAsyncClient:
439 """
440 Get a database connection by name.
441
442 Args:
443 connection_name: The connection alias. If None, uses the default connection.
444 With a single connection, it becomes the default automatically.
445 With multiple connections, either specify explicitly or
446 configure one as "default".
447
448 Returns:
449 The database client for the specified connection.
450
451 Raises:
452 ConfigurationError: If context not initialized, connection not found,
453 or no default connection when multiple exist.
454 """
455 if not self._inited:
456 raise ConfigurationError(
457 "Context not initialized. Call init() before accessing database."
458 )
459
460 if connection_name is None:
461 if self._default_connection is None:
462 raise ConfigurationError(
463 "No default connection configured. Either use a single connection, "
464 "name one 'default', or specify connection_name explicitly."
465 )
466 connection_name = self._default_connection
467
468 return self.connections.get(connection_name)
469
470 async def close_connections(self) -> None:
471 """

Callers

nothing calls this directly

Calls 2

ConfigurationErrorClass · 0.90
getMethod · 0.45

Tested by

no test coverage detected