python 3.x - How to effectively create an alias attribute for a given attribute - Stack Overflow

admin2025-04-19  0

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

Share edited Mar 4 at 9:03 DarkTrick asked Mar 4 at 8:52 DarkTrickDarkTrick 3,5553 gold badges30 silver badges54 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Method 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.
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745053978a282288.html

最新回复(0)