OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to handle circular dependecy between two tables in SQLAlchemy

  • Thread starter Thread starter Peter Kronenberg
  • Start date Start date
P

Peter Kronenberg

Guest
I'm having a problem resolving a circular dependency between two tables. Have followed all the information I found, but it still doesn't quite work.

My tables look like this

Code:
             run                                                calibration_metric
-------------------------------------------           ---------------------------------
run_pk         calibration_metric_pk                    calibration_metric          run_pk
   1                     5                                      5                     1

The relationship from calibration_metric to run is many to 1, but from run to calibration_metric is 1 to 1

I have the following definitions (extraneous fields removed)

Code:
lass Run(ModelBase):
    __tablename__ = 'run'
    pk = mapped_column(Integer, primary_key=True)
    calibration_metric_pk = mapped_column(ForeignKey("calibration_metric.pk", name="fk_run_calibration_metric"))
    metric = relationship("CalibrationMetric",
                                 primaryjoin="calibration_metric_pk == CalibrationMetric.pk",
                                 post_update=True)

lass CalibrationMetric(ModelBase):
    __tablename__ = 'calibration_metric'
    pk = mapped_column(Integer, primary_key=True)
    run_pk = mapped_column(ForeignKey("run.pk", name="fk_calibration_metric_run"))
    run = relationship("Run")

I have this section, "objective_metric_pk == CalibrationMetric.pk", in quotes because without them, Python complained about circular import

I'm getting this error sqlalchemy.exc.InvalidRequestError: When initializing mapper Mapper[Run(run)], expression 'calibration_metric_pk == CalibrationMetric.pk' failed to locate a name ("name 'calibration_metric_pk' is not defined"). If this is a class name, consider adding this relationship() to the <class 'root.db.models.Run.Run'> class after both dependent classes have been defined.

I can't figure out why it's not finding the calibration_metric_pk

Note that I don't care about getting the list of Calibration_Metric objects in Run, that are associated by the Run primary key. I only care about the 1-1 relationship and getting the Calibration_Metric object pointed to by the calibration_metric_pk field. But I would also like to have access to the Run object from Calibration_Metric.run_pk. I don't think I need to specify back_populates, since I only care about 1 end of each of the relationships
<p>I'm having a problem resolving a circular dependency between two tables. Have followed all the information I found, but it still doesn't quite work.</p>
<p>My tables look like this</p>
<pre><code> run calibration_metric
------------------------------------------- ---------------------------------
run_pk calibration_metric_pk calibration_metric run_pk
1 5 5 1
</code></pre>
<p>The relationship from <code>calibration_metric</code> to <code>run</code> is many to 1, but from <code>run</code> to <code>calibration_metric</code> is 1 to 1</p>
<p>I have the following definitions (extraneous fields removed)</p>
<pre><code>lass Run(ModelBase):
__tablename__ = 'run'
pk = mapped_column(Integer, primary_key=True)
calibration_metric_pk = mapped_column(ForeignKey("calibration_metric.pk", name="fk_run_calibration_metric"))
metric = relationship("CalibrationMetric",
primaryjoin="calibration_metric_pk == CalibrationMetric.pk",
post_update=True)

lass CalibrationMetric(ModelBase):
__tablename__ = 'calibration_metric'
pk = mapped_column(Integer, primary_key=True)
run_pk = mapped_column(ForeignKey("run.pk", name="fk_calibration_metric_run"))
run = relationship("Run")
</code></pre>
<p>I have this section, <code>"objective_metric_pk == CalibrationMetric.pk"</code>, in quotes because without them, Python complained about circular import</p>
<p>I'm getting this error
<code>sqlalchemy.exc.InvalidRequestError: When initializing mapper Mapper[Run(run)], expression 'calibration_metric_pk == CalibrationMetric.pk' failed to locate a name ("name 'calibration_metric_pk' is not defined"). If this is a class name, consider adding this relationship() to the <class 'root.db.models.Run.Run'> class after both dependent classes have been defined.</code></p>
<p>I can't figure out why it's not finding the <code>calibration_metric_pk</code></p>
<p>Note that I don't care about getting the list of Calibration_Metric objects in Run, that are associated by the Run primary key. I only care about the 1-1 relationship and getting the Calibration_Metric object pointed to by the calibration_metric_pk field.
But I would also like to have access to the Run object from Calibration_Metric.run_pk. I don't think I need to specify <code>back_populates</code>, since I only care about 1 end of each of the relationships</p>
 

Latest posts

Top