Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6982263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:15:54+00:00 2026-05-27T18:15:54+00:00

thanks in advance for your help. I have two entities, Human and Chimp. Each

  • 0

thanks in advance for your help.

I have two entities, Human and Chimp. Each has a collection of metrics, which can contain subclasses of a MetricBlock, for instance CompleteBloodCount (with fields WHITE_CELLS, RED_CELLS, PLATELETS).

So my object model looks like (forgive the ASCII art):

---------   metrics    ---------------      ----------------------
| Human | ---------->  | MetricBlock | <|-- | CompleteBloodCount |
---------              ---------------      ----------------------
                        ^
---------    metrics    |
| Chimp | --------------
---------

This is implemented with the following tables:

Chimp (id, …)
Human (id, …)

MetricBlock (id, dtype)
CompleteBloodCount (id, white_cells, red_cells, platelets)
CholesterolCount (id, hdl, ldl)

ChimpToMetricBlock(chimp_id, metric_block_id)
HumanToMetricBlock(human_id, metric_block_id)

So a human knows its metric blocks, but a metric block does not know its human or chimp.

I would like to write a query in SQLAlchemy to find all CompleteBloodCounts for a particular human. In SQL I could write something like:

SELECT cbc.id
FROM complete_blood_count cbc
WHERE EXISTS (
   SELECT 1
   FROM human h
       INNER JOIN human_to_metric_block h_to_m on h.id = h_to_m.human_id
   WHERE
       h_to_m.metric_block_id = cbc.id
)

I’m struggling though to write this in SQLAlchemy. I believe correlate(), any(), or an aliased join may be helpful, but the fact that a MetricBlock doesn’t know its Human or Chimp is a stumbling block for me.

Does anyone have any advice on how to write this query? Alternately, are there other strategies to define the model in a way that works better with SQLAlchemy?

Thank you for your assistance.

Python 2.6
SQLAlchemy 0.7.4
Oracle 11g

Edit:

HumanToMetricBlock is defined as:

humanToMetricBlock = Table(
    "human_to_metric_block",
    metadata,
    Column("human_id", Integer, ForeignKey("human.id"),
    Column("metric_block_id", Integer, ForeginKey("metric_block.id")
)

per the manual.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:15:55+00:00Added an answer on May 27, 2026 at 6:15 pm

    Each primate should have a unique ID, regardless of what type of primate they are. I’m not sure why each set of attributes (MB, CBC, CC) are separate tables, but I assume that they have more than one dimension (primate) such as time, otherwise I would only have one giant table.

    Thus, I would structure this problem in the following manner:
    Create a parent object Primate and derive humans and chimps from it. This example is using single table inheritance, though you may want to use joined table inheritance based on their attributes.

    class Primate(Base):
        __tablename__ = 'primate'
        id = Column(Integer, primary_key=True)
        genus = Column(String)
        ...attributes all primates have...
        __mapper_args__ = {'polymorphic_on': genus, 'polymorphic_identity': 'primate'}
    
    class Chimp(Primate):
        __mapper_args__ = {'polymorphic_identity': 'chimp'}
        ...attributes...
    
    class Human(Primate):
        __mapper_args__ = {'polymorphic_identity': 'human'}
        ...attributes...
    
    class MetricBlock(Base):
        id = ...
    

    Then you create a single many-to-many table (you can use an association proxy instead):

    class PrimateToMetricBlock(Base):
        id = Column(Integer, primary_key=True) # primary key is needed!
        primate_id = Column(Integer, ForeignKey('primate.id'))
        primate = relationship('Primate') # If you care for relationships. 
        metricblock_id = Column(Integer, ForeignKey('metric_block.id')
        metricblock = relationship('MetricBlock')
    

    Then I would structure the query like so (note that the on clause is not necessary since SQLAlchemy can infer the relationships automatically since there’s no ambiguity):

    query = DBSession.query(CompleteBloodCount).\
        join(PrimateToMetricBlock, PrimateToMetricBlock.metricblock_id == MetricBlock.id)
    

    If you want to filter by primate type, join the Primate table and filter:

    query = query.join(Primate, Primate.id == PrimateToMetricBlock.primate_id).\
        filter(Primate.genus == 'human')
    

    Otherwise, if you know the ID of the primate (primate_id), no additional join is necessary:

    query = query.filter(PrimateToMetricBlock.primate_id == primate_id)
    

    If you’re only retrieving one object, end the query with:

    return query.first()
    

    Otherwise:

    return query.all()
    

    Forming your model like this should eliminate any confusion and actually make everything simpler. If I’m missing something, let me know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks in advance for your help. I have a need within an application to
Thanks in advance for your help experts. I want to be able to copy
Hi thanks in advance for your help. I'm just getting started learning Python and
First off, thanks in advance for your help. This issue is driving me nuts.
Thanks in advance for your assistance. I have the following exported part: [Export (typeof(INewComponent))]
Thanks in advance for any help you can provide on this issue! I manage
i need your help. I have two forms, Form1 and Form2. In Form1 i
Thanks in advance for your help. I cannot move forward creating or editing fields
I have two entities involved in this issue. A user can have an event
This one has me pretty rattled so I thank you in advance for your

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.