python - Use base class's property/attribute as a table column? -
a game engine provides me player
class steamid
property (coming c++, basic example on in python):
# game_engine.py class player: def __init__(self, steamid): self.__steamid = steamid @property def steamid(self): return self.__steamid
i proceed subclass class while adding gold
attribute:
# my_plugin.py class myplayer(game_engine.player, base): gold = column(integer)
now need store player's gold
database player's steamid
primary key identify player. how tell sqlalchemy use base class's steamid
property primary key?
here's silly tried:
from sqlalchemy.ext.declarative import declarative_base 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 @hybrid_property def steamid(self): return type(self)._steamid.__get__(self)
but yeah, long shot...
sqlalchemy.exc.argumenterror: mapper mapper|player|player not assemble primary key columns mapped table 'player'
this simpler might expect. solution below equivalent 1 r-m-n, more straightforward because uses modern declarative mapping. there no need @hybrid_property
, can inherit steamid
parent class.
# my_plugin.py class myplayer(game_engine.player, base): def __init__(self, steamid, gold): super().__init__(steamid) self._id = self.steamid self.gold = gold _id = column('steamid', integer, primary_key=true) gold = column(integer)
Comments
Post a Comment