Hacker News new | ask | show | jobs
by tomrod 784 days ago
How aware is the library regarding existing structure, e.g foreign key relationships?
2 comments

The library itself provides tools for declaring data tables, columns, metrics, and relationships, then performing transformations (eg. queries) on top of them.

Reflecting an existing database isn't part of the library, but with the magic of "it's just Python" it's pretty easy to write a function that does so yourself:

    import hashquery as hq
    def reflect_db(url: str) -> list[hq.Model]:
      """
      Given a connection string,
      returns a list of Hashquery models
      for all the physical tables in
      the database.
      """
      models: list[hq.Model] = []

      reflection = some_reflection_lib.reflect_db(url)
      for table in reflection.tables:
        models.append(
          hq.Model()
          .with_source(url, table.name, table.schema)
          .with_attributes(*table.column_names)
        )
        # ...more logic for importing foreign keys or whatever else

      return models
Joining logic is defined in our data modeling layer, either from Hashboard (our BI tool) or can also be defined in Hashquery as well instead of replying on your database schema for fk relationships. We went this route since a lot of people are using dbt generated tables.

https://hashquery.dev/docs/pattern_guides/joins/