SQLAlchemy "AttributeError: 'str' object has no attribute 'c'"
SQLAlchemy "AttributeError: 'str' object has no attribute 'c'"
--
Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Industries in Orbit Looping
--
Chapters
00:00 Question
01:39 Accepted answer (Score 29)
02:39 Answer 2 (Score 3)
02:54 Thank you
--
Full question
https://stackoverflow.com/questions/5346...
Accepted answer links:
[here]: https://docs.sqlalchemy.org/en/latest/or...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #sqlalchemy
#avk47
ACCEPTED ANSWER
Score 42
In your UserPermission class, you are using the wrong dunder attribute:
__table__ = 'userPermissions'
Should be:
__tablename__ = 'userPermissions'
Sqlalchemy is trying to treat the string 'userPermissions' as a Table object.
Regarding the difference between __table__ and __tablename__, most cases will only require declaring __tablename__ = "stringvalue" on a declarative class. It signals that the object should reference a table of that name, and SQLAlchemy can handle the construction of that Table object internally.
Declaring a __table__ on the object instead signals to SQLAlchemy that you want to take control of the construction of the Table that the ORM class represents. This would be most useful if you already have a reference to the table from some other means like table reflection. More reading here.
ANSWER 2
Score 3
class UserPermission(Base):
__table__ = 'userPermissions'
here should be :
class UserPermission(Base):
__tablename__ = 'userPermissions'