Skip to content

Schema createa new connection object for every call #14

@circulon

Description

@circulon

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:

  1. Edit the Schema class
  2. edit new_connection() method
  3. add a print(f"new connection created: {self._connection}") before the return statement
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions