tiangolo / sqlmodel

SQL databases in Python, designed for simplicity, compatibility, and robustness.
https://sqlmodel.tiangolo.com/
MIT License
13.7k stars 616 forks source link

previously working model code breaks after fresh environment setup #386

Closed clstaudt closed 1 year ago

clstaudt commented 2 years ago

First Check

Commit to Help

Example Code


def OneToOneRelationship(back_populates):
    return Relationship(
        back_populates=back_populates,
        sa_relationship_kwargs={"uselist": False},
    )

class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    subtitle: str
    website: str
    email: EmailStr
    phone_number: str
    address_id: Optional[int] = Field(default=None, foreign_key="address.id")
    address: Optional[Address] = Relationship(back_populates="users")
    VAT_number: Optional[str]
    # User 1:1* ICloudAccount
    icloud_account_id: Optional[int] = Field(
        default=None, foreign_key="icloudaccount.id"
    )
    icloud_account: Optional["ICloudAccount"] = Relationship(back_populates="user")
    bank_account_id: Optional[int] = Field(default=None, foreign_key="bankaccount.id")
    bank_account: Optional["BankAccount"] = Relationship(back_populates="user")
    logo: Optional[str]

class ICloudAccount(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    user_name: str
    user: User = OneToOneRelationship(back_populates="icloud_account")

def test_user():
    user = model.User(
        name="Archibald Tuttle",
        subtitle="Heating Engineer",
        email="harry@tuttle.com",
    )

    icloud_account = model.ICloudAccount(
        user_name=user.email,
    )

    user.icloud_account = icloud_account

    assert icloud_account.user.name == "Archibald Tuttle"

Description

The above unit test fails with

>       assert icloud_account.user.name == "Archibald Tuttle"
E       AttributeError: 'ICloudAccount' object has no attribute 'user'

What is weird about this is that this is well-tested code that was running before. I set up a new Python environment, but using the same latest version 0.0.6 of sqlmodel. How can I investigate the problem?

Operating System

macOS

Operating System Details

No response

SQLModel Version

0.0.6

Python Version

Python 3.9.13

Additional Context

No response

Apollo314 commented 2 years ago

315 again. Pinning SQLAlchemy to 1.4.35 fixes it at the moment.

clstaudt commented 1 year ago

@Apollo314 Thanks, this fixed the test failures. Hoping for a new release of sqlmodel soon that removes the need to pin sqlalchemy.