Migrated from: MasoniteFramework/orm#958
Original author: @circulon
Originally opened: 2026-03-26
Original state: closed
Describe the bug
Currently the Schema class does not cache the connection but recreates it before every call to the DB
This means that during migrations connection limits to the DB could be exceeded.
There is no get_connection() method on the Schema class which would facilitate connection caching.
this is the new_connection() method as it stands which is used in many Schema.xxx methods.
def new_connection(self):
if self._dry:
return
self._connection = (
self.connection_class(**self.get_connection_information())
.set_schema(self.schema)
.make_connection()
)
return self._connection
To Reproduce
Steps to reproduce the behavior:
- Edit the Schema class
- edit new_connection() method
- add a
print(f"new connection created: {self._connection}") before the return statement
- run a single migration with multiple
self.schema.xxx calls and see similar output to the following
new connection created: <masoniteorm.connections.PostgresConnection.PostgresConnection object at 0x111909d90>
new connection created: <masoniteorm.connections.PostgresConnection.PostgresConnection object at 0x111eca4e0>
new connection created: <masoniteorm.connections.PostgresConnection.PostgresConnection object at 0x111eca4e0>
new connection created: <masoniteorm.connections.PostgresConnection.PostgresConnection object at 0x111eca4e0>
Expected behavior
The same connection object should be used for all schema calls
Screenshots or code snippets
N/A
Desktop (please complete the following information):
- OS: Mac OS
- Version 12.7 Monterey
What database are you using?
- Type: Postgres, MySQL, SQLite
- Version multiple
- Masonite ORM: latest 2.0 and 3.0 (latest commit on 3.x branch)
Additional context
None
Describe the bug
Currently the Schema class does not cache the connection but recreates it before every call to the DB
This means that during migrations connection limits to the DB could be exceeded.
There is no
get_connection()method on the Schema class which would facilitate connection caching.this is the
new_connection()method as it stands which is used in many Schema.xxx methods.To Reproduce
Steps to reproduce the behavior:
print(f"new connection created: {self._connection}")before thereturnstatementself.schema.xxxcalls and see similar output to the followingExpected behavior
The same connection object should be used for all schema calls
Screenshots or code snippets
N/A
Desktop (please complete the following information):
What database are you using?
Additional context
None