Skip to content

RelationshipBuilder fails replicating primary join of one-to-many relationship #237

Open

Description

@carlosvega

I have a group of polymorphic tables with the base being:

class SBase(db.Model): 
	__versioned__ = {}
	__tablename__ = 'sbase'
	element_type = db.Column(db.String(256), nullable=False)
	__mapper_args__ = {
		'polymorphic_on': element_type,
		'polymorphic_identity':'sbase',
		'with_polymorphic': '*'
	}

	id = db.Column(db.Integer, primary_key=True, autoincrement=True)

Then, I have one class that inherits this base table which has a one-to-many relationship with Compartment table.

class SBML_model(SBase):
	__versioned__ = {}
	__tablename__ = 'sbml_model'
	__mapper_args__ = {
		'polymorphic_identity': 'sbml_model',
		'polymorphic_load': 'inline'
	}

	id   = db.Column(db.Integer, db.ForeignKey('sbase.id', name='fk_t_sbml_model_c_id'), primary_key=True)
	model_id = synonym('id')

	compartments = db.relationship('Compartment', cascade='all, delete-orphan',
									primaryjoin="and_(Compartment.model_id == SBML_model.id)")

Compartment table also inhertis such SBase table. Each of these two last tables have different polymorphic identity but still, for relationships I need to provide a primary join so that sqlalchemy can determine the join condition between parent/child tables. (I'm not sure if this could be done in a better way).

class Compartment(SBase):
	__versioned__ = {}
	__tablename__ = 'compartment'
	__mapper_args__ = {
		'polymorphic_identity': 'compartment',
		'polymorphic_load': 'inline'
	}
	
	name = db.Column(db.String(256), unique=False, nullable = False)

	model_id = db.Column(db.Integer, db.ForeignKey('sbml_model.id', name='fk_t_compartment_c_model_id'))
	model = db.relationship("SBML_model", foreign_keys=[model_id])

	id   = db.Column(db.Integer, db.ForeignKey('sbase.id', ondelete="CASCADE", name='fk_t_compartment_c_id'), primary_key=True)

These 3 tables are versioned and when I run the following code (to test behaviour) it fails with:
AttributeError: type object 'CompartmentVersion' has no attribute 'compartment_version_id'

model = SBML_model(id_name='model')
db.session.add(model)
db.session.commit()

assert len(model.compartments) == 0
assert count_versions(model) == 1

compartment = Compartment(model=model, name='comp1')
db.session.add(compartment)
db.session.commit()

assert len(model.compartments) == 1
assert count_versions(model) == 2         # this one fails but assume we continue

assert len(model.versions[0].compartments)   == 0   # here I get the following relationship_builder error
assert len(model.versions[1].compartments)   == 1

Details on the error:

Traceback (most recent call last):
  File "test_versioning.py", line 50, in <module>
    print(len(model.versions[0].compartments))
  File "/Users/carlos.vega/dev/biokb-web/env/lib/python3.7/site-packages/sqlalchemy_continuum/relationship_builder.py", line 227, in relationship
    query = self.query(obj)
  File "/Users/carlos.vega/dev/biokb-web/env/lib/python3.7/site-packages/sqlalchemy_continuum/relationship_builder.py", line 68, in query
    self.criteria(obj)
  File "/Users/carlos.vega/dev/biokb-web/env/lib/python3.7/site-packages/sqlalchemy_continuum/relationship_builder.py", line 90, in criteria
    return self.one_to_many_criteria(obj)
  File "/Users/carlos.vega/dev/biokb-web/env/lib/python3.7/site-packages/sqlalchemy_continuum/relationship_builder.py", line 215, in one_to_many_criteria
    self.one_to_many_subquery(obj),
  File "/Users/carlos.vega/dev/biokb-web/env/lib/python3.7/site-packages/sqlalchemy_continuum/relationship_builder.py", line 36, in one_to_many_subquery
    for pk in primary_keys
  File "/Users/carlos.vega/dev/biokb-web/env/lib/python3.7/site-packages/sqlalchemy_continuum/relationship_builder.py", line 36, in <listcomp>
    for pk in primary_keys
  File "/Users/carlos.vega/dev/biokb-web/env/lib/python3.7/site-packages/sqlalchemy/orm/util.py", line 504, in __getattr__
    attr = getattr(target, key)
AttributeError: type object 'CompartmentVersion' has no attribute 'compartment_version_id'

From my point of view, the expected behaviour should be that the first model version does not have any compartments, but then the second version of the model does have a compartment. However, it querying the version table.

Is there any configuration setting I am missing? Also, is there any way to

Activity

  1. added
    pinned
    on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pinned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions