Markus Meskanen Markus Meskanen - 1 year ago 58
Python Question

Use base class's property/attribute as a table column?

A game engine provides me with a

class with a
property (coming from C++, this is just a basic example on what it would look like in Python):


class Player:

def __init__(self, steamid):
self.__steamid = steamid

def steamid(self):
return self.__steamid

I then proceed to subclass this class while adding a


class MyPlayer(game_engine.Player, Base):
gold = Column(Integer)

Now I need to store the player's
to a database with the player's
as a primary key to identify the player. How do I tell SQLAlchemy to use the base class's
property as the primary key?

Here's something silly I tried:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property

import game_engine

Base = declarative_base()

class Player(game_engine.Player, Base):
__tablename__ = 'player'

_steamid = game_engine.Player.steamid

def steamid(self):
return type(self)._steamid.__get__(self)

But yeah, it was a long shot...

sqlalchemy.exc.ArgumentError: Mapper Mapper|Player|player could not assemble any primary key columns for mapped table 'player'

Answer Source

This is simpler than you might expect. The solution below is roughly equivalent to the one from r-m-n, but more straightforward because it uses modern declarative mapping. There is no need for @hybrid_property, you can just inherit steamid from the parent class.


class MyPlayer(game_engine.Player, Base):

    def __init__(self, steamid, gold):
        self._id = self.steamid = gold

    _id = Column('steamid', Integer, primary_key=True)
    gold = Column(Integer)