I'm using Flask, SQLALchemy, Flask-SQLAlchemy, marshmallow, and datatables to make a user list for a game server for admins, it groups entries by a unique identifier (steam id) and then lists each character for that steam id in a row using datatables, all this information is pulled directly from the mariadb database that the game runs from, in addition to that I have a separate database that I use for the website using binds and it all works nicely. Right now for this user list I'm using marshmallow to serialize the data and to add in profile information from the web database. However I'd like to be able to do server side searching of one of the profile fields (names) so I would need to join the Users table and the Accounts table except they're on separate databases as explained above.
Right now the query is the Character table and the Account table which are both on the same database
characters = Character.query.join(Account).order_by(Account.ID.desc())
If I try to join the User table which is on the web database separate from the game database it tries to look for the User table on the game server database which doesn't exist.
characters = Character.query.join(Account).join(User, User.steam_id == Account.SteamID).order_by(Account.ID.desc())
I've seen conversations in my searching a year ago before giving up on this and just using marshmallow to include the information (but it's not searchable) and there was talk of adding schema to the table which I tried but it never ended up working.
I am using flask-sqlalchemy which handles creating the engine and the session object for sqlalchemy.
flask-sqlalchemy: Trouble joining tables from two databases (different bind keys). Getting error 1146 (see below)
So I went back and found the old post regarding my issue and how they overcame it and tried all they did but it still is looking for them on the same server instead of over different connection binds. as seen from this error:
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1142, "SELECT command denied to user 'lif_1'@'localhost' for table 'user'")
[SQL: SELECT count(*) AS count_1
FROM (SELECT lif_1.`character`.`ID` AS `lif_1_character_ID`, lif_1.`character`.`IsActive` AS `lif_1_character_IsActive`, lif_1.`character`.`Name` AS `lif_1_character_Name`, lif_1.`character`.`LastName` AS `lif_1_character_LastName`, lif_1.`character`.`GeoID` AS `lif_1_character_GeoID`, lif_1.`character`.`AccountID` AS `lif_1_character_AccountID`, lif_1.`character`.`GeoAlt` AS `lif_1_character_GeoAlt`, lif_1.`character`.`OffsetMmX` AS `lif_1_character_OffsetMmX`, lif_1.`character`.`OffsetMmY` AS `lif_1_character_OffsetMmY`, lif_1.`character`.`OffsetMmZ` AS `lif_1_character_OffsetMmZ`, lif_1.`character`.`RaceID` AS `lif_1_character_RaceID`, lif_1.`character`.`Alignment` AS `lif_1_character_Alignment`, lif_1.`character`.`CriminalSecondsLeft` AS `lif_1_character_CriminalSecondsLeft`, lif_1.`character`.`Strength` AS `lif_1_character_Strength`, lif_1.`character`.`StrengthLock` AS `lif_1_character_StrengthLock`, lif_1.`character`.`Agility` AS `lif_1_character_Agility`, lif_1.`character`.`AgilityLock` AS `lif_1_character_AgilityLock`, lif_1.`character`.`Intellect` AS `lif_1_character_Intellect`, lif_1.`character`.`IntellectLock` AS `lif_1_character_IntellectLock`, lif_1.`character`.`Willpower` AS `lif_1_character_Willpower`, lif_1.`character`.`WillpowerLock` AS `lif_1_character_WillpowerLock`, lif_1.`character`.`Constitution` AS `lif_1_character_Constitution`, lif_1.`character`.`ConstitutionLock` AS `lif_1_character_ConstitutionLock`, lif_1.`character`.`RootContainerID` AS `lif_1_character_RootContainerID`, lif_1.`character`.`EquipmentContainerID` AS `lif_1_character_EquipmentContainerID`, lif_1.`character`.`HardHP` AS `lif_1_character_HardHP`, lif_1.`character`.`HardStam` AS `lif_1_character_HardStam`, lif_1.`character`.`SoftHP` AS `lif_1_character_SoftHP`, lif_1.`character`.`SoftStam` AS `lif_1_character_SoftStam`, lif_1.`character`.`Luck` AS `lif_1_character_Luck`, lif_1.`character`.`HungerRate` AS `lif_1_character_HungerRate`, lif_1.`character`.`AlchemyHash` AS `lif_1_character_AlchemyHash`, lif_1.`character`.`VisibilityHash` AS `lif_1_character_VisibilityHash`, lif_1.`character`.appearance AS lif_1_character_appearance, lif_1.`character`.`GuildID` AS `lif_1_character_GuildID`, lif_1.`character`.`GuildRoleID` AS `lif_1_character_GuildRoleID`, lif_1.`character`.`TitleMessageID` AS `lif_1_character_TitleMessageID`, lif_1.`character`.`BindedObjectID` AS `lif_1_character_BindedObjectID`, lif_1.`character`.`RallyObjectID` AS `lif_1_character_RallyObjectID`, lif_1.`character`.`LastTimeUsedPraiseYourGodAbility` AS `lif_1_character_LastTimeUsedPraiseYourGodAbility`, lif_1.`character`.`LastTimeUsedTransmuteIntoGold` AS `lif_1_character_LastTimeUsedTransmuteIntoGold`, lif_1.`character`.`CreateTimestamp` AS `lif_1_character_CreateTimestamp`, lif_1.`character`.`DeleteTimestamp` AS `lif_1_character_DeleteTimestamp`
FROM lif_1.`character` INNER JOIN lif_1.account ON lif_1.account.`ID` = lif_1.`character`.`AccountID` INNER JOIN fightclub.user ON fightclub.user.steam_id = lif_1.account.`SteamID` ORDER BY lif_1.account.`ID` DESC) AS anon_1]
(Background on this error at: http://sqlalche.me/e/13/e3q8)
So the select statement works but it's still looking on just one database and ignoring the binds.
class User(SurrogatePK, Model, UserMixin):
"""A user of the app."""
__tablename__ = 'user'
__table_args__ = {'schema': 'fightclub'}
__bind__ = 'fightclub'
steam_id = Column(db.String(40))
...
class Account(db.Model):
__tablename__ = 'account'
__bind_key__ = 'feudal'
__table_args__ = {'schema': 'lif_1'}
ID = db.Column(db.Integer, primary_key=True)
IsActive = db.Column(db.Integer, nullable=False)
IsGM = db.Column(db.Integer, nullable=False)
SteamID = db.Column(db.BigInteger, nullable=False, unique=True)