sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

AttributeError: 'XXXXX' object has no attribute 'YYYYY' only in aws lambda function

Open ivanyschen opened this issue 3 years ago • 2 comments

First Check

  • [X] I added a very descriptive title to this issue.
  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the SQLModel documentation, with the integrated search.
  • [X] I already searched in Google "How to X in SQLModel" and didn't find any information.
  • [X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [X] I already checked if it is not related to SQLModel but to Pydantic.
  • [X] I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

class CoursePostgresORM(SQLModel, table=True):
    __tablename__ = 'courses'
    __table_args__ = {'schema': 'course_catalog', }
    id_: str = Field(alias='id', sa_column=Column('id', primary_key=True))

    regions: List['CourseRegionLinkPostgresORM'] = Relationship(back_populates='course')
    
class RegionPostgresORM(SQLModel, table=True):
    __tablename__ = 'regions'
    __table_args__ = {'schema': 'public', }
    id_: int = Field(sa_column=Column('id', primary_key=True))

    courses: List['CourseRegionLinkPostgresORM'] = Relationship(back_populates='region')

class CourseRegionLinkPostgresORM(SQLModel, table=True):
    __tablename__ = 'courses_regions'
    __table_args__ = {'schema': 'course_catalog', }
    course_id: str = Field(foreign_key='course_catalog.courses.id', primary_key=True)
    region_id: int = Field(foreign_key='public.regions.id', primary_key=True)
    modified_at: datetime
    created_at: datetime

    course: 'CoursePostgresORM' = Relationship(back_populates='regions')
    region: 'RegionPostgresORM' = Relationship(back_populates='courses')


def lambda_handler(event, context):
    """
    This LF responsible for syncing joint tables.
    """
    LOGGER.info(msg='Updating course-region table...')
    with Session(POSTGRES) as session:
        results = session.exec(select(CourseRegionLinkPostgresORM))
        first_row = results.fetchall()[0]
        print(first_row.region)

Description

I encountered some issue where I was thrown with AttributeError: 'XXXXX' object has no attribute 'YYYYY' while trying to access an Relationship attribute of a model in aws lambda function. I was able to run the above code fine on my local but it would produced the following error in an AWS lambda function. AttributeError: 'CourseRegionLinkPostgresORM' object has no attribute 'region'. Any thoughts are welcome. I've got stuck for the whole day. Thanks!

Operating System

Other

Operating System Details

AWS lambda function

SQLModel Version

0.0.6

Python Version

3.7

Additional Context

No response

ivanyschen avatar May 06 '22 04:05 ivanyschen

What version of SQLAlchemy is being used in AWS Lambda? Perhaps you bumped into https://github.com/tiangolo/sqlmodel/issues/315?

byrman avatar May 06 '22 06:05 byrman

What version of SQLAlchemy is being used in AWS Lambda? Perhaps you bumped into https://github.com/tiangolo/sqlmodel/issues/315?

@byrman , thanks for pointing me to it! By pinning my sqlalchemy to .34, the code works fine again!

ivanyschen avatar May 06 '22 15:05 ivanyschen