Is it possible to create a relationship via the table name as the "foreign 
key"? I tried playing around with the foreign and remote options and tried 
utilizing what's described here: 
https://docs.sqlalchemy.org/en/13/orm/join_conditions.html#non-relational-comparisons-materialized-path
 but 
I couldn't get it to work for my case.

I have a table that has entity types and a table_name column, e.g.

class EntityType(Base):
    __tablename__ = "entity_type"

    id = Column(UUID, primary_key=True, server_default=FetchedValue())
    table_name = Column(String, nullable=False)
    prefix = Column(Text, unique=True, nullable=False)
    alt_prefix = Column(Text)
    ui_label = Column(Text, unique=True, nullable=False)
    entry_key = Column(Text, unique=True, nullable=False)

    config_entity_column_ui_visibility = 
relationship("ConfigEntityColumnUiVisibility")

    def __repr__(self):
        return (
            f"<EntityType(id={self.id} table_name={self.table_name} 
prefix={self.prefix} ui_label={self.ui_label} "
            f"entry_key={self.entry_key}>"
        )


I want to create a relationship to this table from other tables via their table 
name rather than a column on the table.  Is this possible?


e.g.

class GenericEntityTypeMixin:

    @declared_attr.cascading
    def prefix(cls) -> ColumnProperty:
        from webapp.database.orm.models import EntityType

        return column_property(
            select([EntityType.prefix])
                .where(EntityType.table_name == cls.__tablename__)
                .as_scalar(),
            info={"key": "prefix"},
        )

    @hybrid_property
    def qualified_id(self):
        return f"{self.prefix}-{self.visible_id}"

    # @declared_attr
    # def entity_type(cls) -> RelationshipProperty:
    #     how do we create relationship without using a column object on the 
foreign side?

    @declared_attr
    def entity_type_entry_key(cls):
        return association_proxy("entity_type", "entry_key")

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/d54d0af1-7e8e-44f1-8398-989b09b3ea8e%40googlegroups.com.

Reply via email to