python-3.xsqlalchemymigration

How to effectively create an alias attribute for a given attribute


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


Solution

  • 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)
    session.commit()
    

    Reading

    session.query(User).filter(
        User.user_id == "user1"
      ).one()
    

    Note: