Re: [sqlalchemy] Inherit foreign key to a dynamic table based on inherited column value
I wasnt looking to use the monster,race,foliage, and fauna classes as mixins for the character ones. There's not a monster, or fauna stat that i want to add on to a character object. Didnt realize this is what I'm trying to do until you asked the right questions. I guess I'm trying to make it like this: - Entity heirarcy is supposed to be used for mechanics of combat, and information about specific characters/bosses/players. - Species hierarcy is supposed to be more of a lore storage table. Information pertaining to a specific bread of monster, fauna, race etc... I could easily put all the species in one table and do a simple 1:M, but the monsters will probably need more columns than the foilage table, and that will waste space. - The relationship i need is to basically hop straight from a specific character to the lore entry about its species. There currently is no base of those species classes. SpeciesLookup()was performing that function until I changed it to just be a mapping of ids to the table names. But making a base class for them is a good idea, so I'll try it out next time I'm at my computer. Thanks a bunch, man. Ur youtube vid and pdf handout with the tutorial really kick started me into this. Im lovin it. On Thu, Apr 5, 2018, 08:45 Mike Bayer <mike...@zzzcomputing.com> wrote: > when you have CharacterEntity, would there also be > MonsterCharacterEntity, FaunaCharacterEntity, etc. ? if you had > "Monster", "Fauna", "Race", "Foliage" mixins that are applied to the > LivingEntity hierarchy that would be a place to hold your > "species_lookup" relationship. What is the base of the hierarchy of > RaceLookup/MonsterLookup/FaunaLookup/FoliageLookup? there should be > some common term for that too like TaxonomyLookup or something. since > we are heavy with entity terminology here. > > On Thu, Apr 5, 2018 at 8:52 AM, Johnathan Norton > <ancientradiostat...@gmail.com> wrote: > > Disclaimer: I'm very new at this. Only been learning databases for a > couple > > weeks, and been working with python for about a month and 1/2. I did do > > several searches, phrasing things differently, but I can't seem to find > my > > answer. > > > > I'm trying to build a overly complex DnD database and cmdline program to > GM > > with. > > Current issue is that I'm playing with a lot of mixin classes, creating > new > > tables for more and more complex entities so I'm not having simple > > entities(less columns) waste space in the same table with complex > ones(more > > columns). > > > > For instance: > > LivingEntity table has current_hp, max_hp, and base_ac columns. > > StattedEntity would inherit those from LivingEntity and gain attributes > that > > ready it for combat: str, dex, con, etc... > > > > I'm trying to add a piece of functionality to the LivingEntity class that > > gets passed down to its children. I want all children entity tables to > have > > two columns. > > - One column is the entity's species type (Foliage, Fauna, Monster, > > Race) > > - The other column takes that type and goes to a table with that > type in > > the name, and uses the second column's entry as a foreign key for that > > column. > > > > When I create an entity, I'd like to be able to give a function a > string. It > > searches through the fauna, foliage, monster, and race tables for that > > string, and when it finds it, fills in the species_id, and species_type > > columns appropriately. > > Finally, I want to be able to query the foliage, fauna, monster, and race > > tables to get general information about trees, deer, beholders, or > humans. > > > > Code: (Bolded text is my main focus atm.) > > > > class Entity(): > > __tablename__ = 'Entity' > > > > id > > name > > description > > parent_campaign > > > > class LivingEntity(Entity): > > """Parent class for statted entities. Mainly basic metadata about > > generic living creatures that won't be in combat.""" > > __tablename__ = 'LivingEntities' > > > > @declared_attr > > def species_type(cls): > > return Column('species_type', ForeignKey('SpeciesLookup.name'), > > nullable=False) > > > > @declared_attr > > def species_id(cls): > > Column('species_id', ForeignKey(f'{species_type}Lookup.id')) > > #species_type is out of scope here, yes? > > > > current_hp = Column(Integer, nullable=False) > > max_hp = Column(Integer, nullable=False) > > base_ac = Column(Integer, nullab
[sqlalchemy] Inherit foreign key to a dynamic table based on inherited column value
Disclaimer: I'm very new at this. Only been learning databases for a couple weeks, and been working with python for about a month and 1/2. I did do several searches, phrasing things differently, but I can't seem to find my answer. I'm trying to build a overly complex DnD database and cmdline program to GM with. Current issue is that I'm playing with a lot of mixin classes, creating new tables for more and more complex entities so I'm not having simple entities(less columns) waste space in the same table with complex ones(more columns). For instance: LivingEntity table has current_hp, max_hp, and base_ac columns. StattedEntity would inherit those from LivingEntity and gain attributes that ready it for combat: str, dex, con, etc... I'm trying to add a piece of functionality to the LivingEntity class that gets passed down to its children. I want all children entity tables to have two columns. - One column is the entity's species type (Foliage, Fauna, Monster, Race) - The other column takes that type and goes to a table with that type in the name, and uses the second column's entry as a foreign key for that column. When I create an entity, I'd like to be able to give a function a string. It searches through the fauna, foliage, monster, and race tables for that string, and when it finds it, fills in the species_id, and species_type columns appropriately. Finally, I want to be able to query the foliage, fauna, monster, and race tables to get general information about trees, deer, beholders, or humans. Code: (Bolded text is my main focus atm.) class Entity(): __tablename__ = 'Entity' id name description parent_campaign class LivingEntity(Entity): """Parent class for statted entities. Mainly basic metadata about generic living creatures that won't be in combat.""" __tablename__ = 'LivingEntities' @declared_attr def species_type(cls): return Column('species_type', ForeignKey('SpeciesLookup.name'), nullable=False) *@declared_attrdef species_id(cls): Column('species_id', ForeignKey(f'{species_type}Lookup.id')) #species_type is out of scope here, yes?* current_hp = Column(Integer, nullable=False) max_hp = Column(Integer, nullable=False) base_ac = Column(Integer, nullable=False) @declared_attr def SpeciesLookup(cls): return relationship('SpeciesLookup') *@declared_attrdef I_don't_even_know_how_to_begin_to_define_this_relationship(cls):* *return relationship(f'{species_type}Lookup')* class StattedEntity(LivingEntity): """Parent class for monster, character, and player tables. Adds combat and hex map functionality.""" __tablename__ = 'StattedEntity' level armor_ac_bonus init_bonus initiative speed etc... class SentientEntity(): """Parent class for any entities needing sentient functionality.""" __tablename__ = 'SentientEntity' alignment religion behaviors current_goal fears class CharacterEntity(StattedEntity, SentientEntity, Base): """Holds instances of simple NPCs.""" profession talents brief_backstory party stress main_goal class PlayerEntity(StattedEntity, SentientEntity, Base): """Holds instances of player characters.""" class_id backstory_file storybook_file wis_save cha_save athletics acrobatics sleight_of_hand stealth location etc... class SpeciesLookup(Entity, Base): """Lookup table for species types.""" __tablename__ = 'SpeciesLookup' class FoliageLookup(Entity, Base): """lookup table for all foliage species""" __tablename__ = 'FoliageLookup' specific_attribute_1 specific_attribute_3 class FaunaLookup(Entity, Base): """Lookup table for all innocent fauna""" __tablename__ = 'FaunaLookup' specific_attribute_5 specific_attribute_2 class MonsterLookup(Entity, Base): """Lookup table for all monsters, beasts""" __tablename__ = 'MonsterLookup' specific_attribute_7 specific_attribute_6 class RaceLookup(Entity, Base): """lookup table for all races""" __tablename__ = 'RaceLookup' specific_attribute_9 specific_attribute_11 -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.