Replies: 6 comments
-
|
A Quick glance at this tells me that it's a SQLAlchemy Error, and not a FastAPI Error or Bug, Perhaps check the SQLAlchemy Documentation for it? |
Beta Was this translation helpful? Give feedback.
-
I looked at the documentation, but they create the models and add them one by one in the examples. I am trying to understand the method used in the fastapi project generator with postgres and how to modify it to update nested relationships in the orm. |
Beta Was this translation helpful? Give feedback.
-
|
OK, I see the issue here. I think you're misunderstanding what a With that out of the way! A Take a look at the dictionary you've obtained from
This isn't what you want, obviously! What you can do is to create an instance of the Model that you are trying to update! We can obtain a Record by Querying it from the database: Note that the filter used here is completely arbitrary. Then Update Each of those fields? for (Key, Value) in obj_in.items():
setattr(rec, Key, Value) # Note, do not ever actually do this. I'm doing to remain very close to your original code.
# Updated? Flush and Commit the record.
db.commit() As a side note, this really is a problem with SQLAlchemy (Not a bug, rather the usage), and the lovely, wonderful and absolutely soothing documentation over at SQLAlchemy Official Documentation is a great place to start. It is very through and gives you examples to follow along if you do not understand. Please follow the Examples from FastAPI Main Documentation as well, they are most helpful in Integrating SQLAlchemy in FastAPI. |
Beta Was this translation helpful? Give feedback.
-
|
Hi, after looking at SQLAlchemy I realized that for having many to many relations like """
file: user.py
"""
class User(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True, index=True)
full_name = Column(String, index=True)
email = Column(String, unique=True, index=True, nullable=False)
hashed_password = Column(String, nullable=False)
is_active = Column(Boolean(), default=True)
is_superuser = Column(Boolean(), default=False)
items = relationship("Role", secondary=UserRole.__tablename__, back_populates="user")"""
file: role.py
"""
class Role(Base):
__tablename__ = 'role'
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
is_primary = Column(Boolean)
items = relationship("User", secondary=UserRole.__tablename__, back_populates="role")"""
file: user_role.py
"""
class UserRole(Base):
__tablename__ = 'user_role'
user_id = Column('user_id', ForeignKey('user.id'), primary_key=True)
role_id = Column('role_id', ForeignKey('role.id'), primary_key=True)don't forget to import UserRole class in User and Role classes |
Beta Was this translation helpful? Give feedback.
-
|
the problem is that |
Beta Was this translation helpful? Give feedback.
-
I have established a many-to-many relationship here in FAST-SQLAlchemy, but not the slightest idea of how to generate the insert and update data. Any suggestion **I have established a many-to-many relationship here in FAST-SQLAlchemy, but not the slightest idea of how to generate the insert and update data. Any suggestion I get this error; dem would like an orientation thank you** |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
First check
Hello everyone. I am having a really hard time updating a user with a many to many relationship.
The models used are these.
All the models have Config orm=True
The usage is that I have an admin panel where I can add roles to a profile. So I get the json model to the Vue app, add new roles and then post it to the api. When this updated model tries to set the roles attribute it fails.
Now after the endpoint is called. I see the new roles in the pydantic model as expected. But when the crud update tries to apply the new roles in the list it fails.
But when it reaches this point
When I try to update user profile and add more roles to the list. I get the following error.
backend_1 | File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 45, in append
backend_1 | item_state = attributes.instance_state(item)
backend_1 | AttributeError: 'dict' object has no attribute '_sa_instance_state'
Any idea what I can be doing incorrectly ?
Using fastapi = "^0.60.1" and Python 3.8
Thanks
Beta Was this translation helpful? Give feedback.
All reactions