Python 如何使用SQLAlchemy建立表之间的关系?

Python 如何使用SQLAlchemy建立表之间的关系?,python,orm,sqlalchemy,relation,Python,Orm,Sqlalchemy,Relation,使用SQLAlchemy,给定如下表: locations_table = Table('locations', metadata, Column('id', Integer, primary_key=True), Column('name', Text), ) players_table = Table('players', metadata, Column('id', Integer, primary_key=True)

使用SQLAlchemy,给定如下表:

locations_table = Table('locations', metadata,
    Column('id',        Integer, primary_key=True),
    Column('name', Text),
)

players_table = Table('players', metadata,
    Column('id',                 Integer, primary_key=True),
    Column('email',           Text),
    Column('password',   Text),
    Column('location_id',  ForeignKey('locations.id'))
)
class Location(object):
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return '<Location: %s, %s>' % (self.name)

mapper(Location, locations_table)

class Player(object):
    def __init__(self, email, password, location_id):
        self.email = email
        self.password = password
        self.location_id = location_id

    def __repr__(self):
        return '<Player: %s>' % self.email

mapper(Player, players_table)
# assign location to player using a Location object, as opposed to an ID
player.location = location
# access the Location object associated with the player directly
print player.location.name
以及以下类别:

locations_table = Table('locations', metadata,
    Column('id',        Integer, primary_key=True),
    Column('name', Text),
)

players_table = Table('players', metadata,
    Column('id',                 Integer, primary_key=True),
    Column('email',           Text),
    Column('password',   Text),
    Column('location_id',  ForeignKey('locations.id'))
)
class Location(object):
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return '<Location: %s, %s>' % (self.name)

mapper(Location, locations_table)

class Player(object):
    def __init__(self, email, password, location_id):
        self.email = email
        self.password = password
        self.location_id = location_id

    def __repr__(self):
        return '<Player: %s>' % self.email

mapper(Player, players_table)
# assign location to player using a Location object, as opposed to an ID
player.location = location
# access the Location object associated with the player directly
print player.location.name
简化的

我将如何修改它以支持以下操作:

locations_table = Table('locations', metadata,
    Column('id',        Integer, primary_key=True),
    Column('name', Text),
)

players_table = Table('players', metadata,
    Column('id',                 Integer, primary_key=True),
    Column('email',           Text),
    Column('password',   Text),
    Column('location_id',  ForeignKey('locations.id'))
)
class Location(object):
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return '<Location: %s, %s>' % (self.name)

mapper(Location, locations_table)

class Player(object):
    def __init__(self, email, password, location_id):
        self.email = email
        self.password = password
        self.location_id = location_id

    def __repr__(self):
        return '<Player: %s>' % self.email

mapper(Player, players_table)
# assign location to player using a Location object, as opposed to an ID
player.location = location
# access the Location object associated with the player directly
print player.location.name
如果炼金术允许:

# print all players having a certain location
print location.players

?使用sqlalchemy的关系功能:


使用sqlalchemy的关系功能:


这应该适合您:

mapperPlayer,players\u表,属性={'location'=relationLocation,uselist=False,backref=backref'players'}


这样您就可以直接访问该位置,因为您不会得到列表。除此之外,您还可以执行location.players,它将返回一个InstrumentedList,这样您就可以对播放器进行测试,这应该对您有用:

mapperPlayer,players\u表,属性={'location'=relationLocation,uselist=False,backref=backref'players'}

这样您就可以直接访问该位置,因为您不会得到列表。除此之外,您还可以执行location.players,它将返回一个InstrumentedList,这样您就可以对播放器进行测试