Skip to content

Nullable PasswordType is not updated when original value is None #777

@om-henners

Description

@om-henners

When using the PasswordType with on a nullable field, if the field is null to start with and you try to update the value, no changes are reflected in the DB.

For example with a simple definition:

from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy_utils import PasswordType

class Base(DeclarativeBase):
    pass

class User(Base):
    __tablename__ = "user"

    id: Mapped[int] = mapped_column(primary_key=True)
    password: Mapped[bytes] = mapped_column(PasswordType, nullable=True)

Then this works as expected:

user_1 = User(password="super_secret")
session.add(user_1)
session.commit()

# update password
user_1.password = "swordfish"
session.add(user_1)
session.commit()
session.refresh(user_1)

# check it worked
assert user_1.password == 'swordfish'

But this fails:

user_2 = User(password=None)
session.add(user_2)
session.commit()

# update password
user_2.password = "swordfish"
session.add(user_2)
session.commit()
session.refresh(user_2)

# check it worked
assert user_2.password == 'swordfish'

Using:

  • Python 3.11
  • sqlalchemy 2.0.40
  • sqlalchemy-utils 0.41.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions