Given the following definition for my SqlAlchemy class:
class User(Base):
id = Column("user_id", String(60), primary_key=True)
user_name = Column("user_name", String(60), nullable=False)
how can I create an attribute user_id
that maps to id
.
Background:
I'd like to migrate my code from using id
to using user_id
. However, there are several system accessing this class, so for a certain amount of time I need both names to be used in queries and for creation
Given the following definition for my SqlAlchemy class:
class User(Base):
id = Column("user_id", String(60), primary_key=True)
user_name = Column("user_name", String(60), nullable=False)
how can I create an attribute user_id
that maps to id
.
Background:
I'd like to migrate my code from using id
to using user_id
. However, there are several system accessing this class, so for a certain amount of time I need both names to be used in queries and for creation
1 Answer
Reset to default 0Method 1: synonym (v1.4, v2.0)
You can use the synonym function:
from sqlalchemy.orm import synonym
class User(Base):
id = Column("user_id", String(60), primary_key=True)
user_name = Column("user_name", String(60), nullable=False)
user_id = synonym("id")
Method 2: hybrid_property (v1.4, v2.0)
You can use the hybrid_property
decorator , which basically works like a property
in Python.
In your case:
from sqlalchemy.ext.hybrid import hybrid_property
class User(Base):
id = Column("user_id", String(60), primary_key=True)
user_name = Column("user_name", String(60), nullable=False)
@hybrid_property
def user_id(self):
return self.id
@user_id.setter
def user_id(self, user_id: str):
self.id = user_id
Example of using:
Writing
user = User()
user.user_id = "user1"
user.user_name = "a name"
session.add(user)
sessionmit()
Reading
session.query(User).filter(
User.user_id == "user1"
).one()
Note:
- Your Python linter might mark
user_id
as invalid re-declaration. This should be a linter problem. it's no problem when actually running.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745053976a4608555.html
评论列表(0条)