Skip to content

JSON casts not working in version 2.x #951

@bedus-creation

Description

@bedus-creation

Masonite ORM casts not working for json column in 2.x

To Reproduce
Here is the simple tinker.py that can run to see the results

import os

from dotenv import load_dotenv

import masoniteorm

load_dotenv('/Users/ellite/code/packages/masonite-orm/.env')
from masoniteorm.migrations import Migration
from masoniteorm.models.Model import Model


class MigrationForUsersTable(Migration):
    def up(self):
        """
        Run the migrations.
        """
        with self.schema.create("users") as table:
            table.increments('id')
            table.jsonb('profile').nullable()

            table.timestamps()

    def down(self):
        """
        Revert the migrations.
        """
        self.schema.drop("users")


class User(Model):
    __connection__ = "postgres"
    __casts__ = {
        "profile": "json"
    }


MigrationForUsersTable(connection='postgres').down()
MigrationForUsersTable(connection='postgres').up()
user = User.create({"profile": {"name": "Ellite"}})
user.update({"profile": {"name": "Ellite2"}})

Expected behavior
The above code should run and save the profile as json

Screenshots or code snippets

Traceback (most recent call last):
  File "/Users/ellite/code/packages/masonite-orm/src/masoniteorm/connections/PostgresConnection.py", line 214, in query
    self.statement(query, bindings)
  File "/Users/ellite/code/packages/masonite-orm/src/masoniteorm/connections/BaseConnection.py", line 43, in statement
    self._cursor.execute(query, bindings)
  File "/Users/ellite/code/packages/masonite-orm/.venv2x/lib/python3.12/site-packages/psycopg2/extras.py", line 236, in execute
    return super().execute(query, vars)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.ProgrammingError: can't adapt type 'dict'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/ellite/Library/Application Support/JetBrains/PyCharm2025.2/scratches/orm.py", line 39, in <module>
    user = User.create({"profile": {"name": "Ellite"}})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ellite/code/packages/masonite-orm/src/masoniteorm/models/Model.py", line 568, in create
    return cls.builder.create(dictionary, cast=cast, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ellite/code/packages/masonite-orm/src/masoniteorm/query/QueryBuilder.py", line 544, in create
    query_result = connection.query(self.to_qmark(), self._bindings, results=1)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ellite/code/packages/masonite-orm/src/masoniteorm/connections/PostgresConnection.py", line 222, in query
    raise QueryException(str(e)) from e
src.masoniteorm.exceptions.QueryException: can't adapt type 'dict'

Desktop (please complete the following information):

  • OS: Mac

What database are you using?

  • Type: Postgres
  • Version 17
  • Masonite ORM 2.0

Additional context
I additionally tested with the dev version 3.0, it's working fine

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn existing feature is not working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions