#Nested Models/Relationship/SQLAlchemy issue

1 messages · Page 1 of 1 (latest)

calm niche
#

I'm experiencing an issue with nested models.

Here is my POST JSON:

{ "name": "some string value", "tag": { "tag_id": 5, "value": null } }
"name": "some string value",

is in TableA

"tag": { "tag_id": 5, "value": null }
is in TableB and it has a reference to TableA

`_data = data.model_dump(exclude_unset=True, by_alias=False, exclude_none=True)

get json element as a class

tag: MetaDataTagValue = _data.pop('tag')
saved_line: MetaDataLine = await meta_data_line_repo.add(MetaDataLine(**_data))
await meta_data_line_repo.session.commit()

add foreign key of the record that was just saved

tag.update({'line_id': saved_line.id})

everything is fine up until the next line with '.add(**tag)'

saved_tag: MetaDataTagValue = await meta_data_tag_value_repo.add(**tag)
await meta_data_tag_value_repo.session.commit()`

I get the following error message
"detail": "SQLAlchemyAsyncRepository.add() got an unexpected keyword argument 'tag_id'"
However, tag_id is part of the MetaDataTagValue structure.

I know that I doing something incorrectly, but I do not know what.
Can you help me out or maybe point me in the correct direction?

I can post a link to my GitHub with all of this, plus more if you'd like.

Side Note:
This has been an ongoing issue for me for the last two weeks or so and I've tried many different things, sometimes ending up with an error message saying something like "dict does not have a sa_attribite".

plush spireBOT
#
Notes for Nested Models/Relationship/SQLAlchemy issue
At your assistance

@calm niche

No Response?

If no response in a reasonable time, ping @Member.

Closing

To close, type !solve or byte solve.

MCVE

Please include an MCVE so that we can reproduce your issue locally.

compact hull
#

You are going to have to convert the dictionary of kwargs (**tag) to a SQLAlchemy model

#

Alternately, you can use the service layer to help you with this.

#

This example has exactly what you are looking for

acoustic shuttleBOT
#

src/app/domain/teams/services.py line 73

async def create(self, data: Team | dict[str, Any]) -> Team:
compact hull
acoustic shuttleBOT
#

src/app/domain/teams/services.py line 121

tags_service = await anext(provide_tags_service(db_session=cast("AsyncSession", self.repository.session)))
magic condor
#

byte solve