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 8076499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:16:23+00:00 2026-06-05T15:16:23+00:00

I’m designing a database to house scientific test data, using sqlalchemy. I’ve hit a

  • 0

I’m designing a database to house scientific test data, using sqlalchemy. I’ve hit a problem that I can’t seem to figure out.

In my test data, each Observation has a State (position, velocity, acceleration), and a State has an associated Time (time at which the state applies). So far, so good. I made a separate table for Times because I deal with different kinds of times, and I wanted to use a reference table to indicate what kind of time each time is (state time, observation time, etc). And the types of times I deal with might change, so normalizing in this way I think will let me add new kinds of times in the future, since they’re just rows in a reference table.

So far this part works (using declarative style):

class Observation(Base):
    __tablename__ = 'tbl_observations'
    id = Column(Integer, primary_key=True)
    state_id = Column(Integer, ForeignKey('tbl_states.id'))
    state = relationship('State', uselist=False)

class State(Base):
    __tablename__ = 'tbl_states'
    id = Column(Integer, primary_key=True)
    time_id = Column(Integer, ForeignKey('tbl_times.id'))
    time = relationship('Time', uselist=False)

class Time(Base):
    __tablename__ = 'tbl_times'
    id = Column(Integer, primary_key=True)
    time_type_id = Column(Integer, ForeignKey('ref_tbl_time_types.id'))
    time_type = relationship('TimeType', uselist=False)
    time_value = Column(Float)

class TimeType(Base):
    __tablename__ = 'ref_tbl_time_types'
    id = Column(Integer, primary_key=True)
    desc = Column(String)

The wrinkle is that observations themselves can have different kinds of times. When I try to create a one-to-many relationship between Observation and Time, I get a circular dependency error:

class Observation(Base):
    __tablename__ = 'tbl_observations'
    id = Column(Integer, primary_key=True)
    state_id = Column(Integer, ForeignKey('tbl_states.id'))
    state = relationship('State', uselist=False)

    # Added this line:
    times = relationship('Time')

class Time(Base):
    __tablename__ = 'tbl_times'
    id = Column(Integer, primary_key=True)
    time_type_id = Column(Integer, ForeignKey('ref_tbl_time_types.id'))
    time_type = relationship('TimeType', uselist=False)
    time_value = Column(Float)

    # Added this line:
    observation_id = Column(Integer, ForeignKey('tbl_observations.id'))

I’m guessing this breaks because the original Observation -> State -> Time chain has a reference right back up to Observation.

Is there any way to fix this? Have I gotten my design all screwed up? Am I doing something wrong in sqlalchemy? I’m new to all of this so it could be any of the above. Any help you can give would be very much appreciated.

P.S. I tried doing what was recommended here: Trying to avoid a circular reference but either I did it wrong or it didn’t solve my particular problem.

  • 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-06-05T15:16:25+00:00Added an answer on June 5, 2026 at 3:16 pm

    The other answers here regarding reconsideration of your use case are valuable, and you should consider those. However, as far as SQLAlchemy is concerned, the circular dependency issue due to multiple FKs is solved by the use_alter/post_update combination, documented at http://docs.sqlalchemy.org/en/rel_0_7/orm/relationships.html#rows-that-point-to-themselves-mutually-dependent-rows . Here is the model using that:

    from sqlalchemy import *
    from sqlalchemy.orm import *
    from sqlalchemy.ext.declarative import declarative_base
    
    Base= declarative_base()
    
    class Observation(Base):
        __tablename__ = 'tbl_observations'
        id = Column(Integer, primary_key=True)
        state_id = Column(Integer, ForeignKey('tbl_states.id'))
        state = relationship('State', uselist=False)
    
        times = relationship('Time')
    
    class State(Base):
        __tablename__ = 'tbl_states'
        id = Column(Integer, primary_key=True)
        time_id = Column(Integer, ForeignKey('tbl_times.id'))
    
        # post_update is preferable on the many-to-one
        # only to reduce the number of UPDATE statements
        # versus it being on a one-to-many.
        # It can be on Observation.times just as easily.
        time = relationship('Time', post_update=True)
    
    class Time(Base):
        __tablename__ = 'tbl_times'
        id = Column(Integer, primary_key=True)
        time_type_id = Column(Integer, ForeignKey('ref_tbl_time_types.id'))
        time_type = relationship('TimeType', uselist=False)
        time_value = Column(Float)
    
        observation_id = Column(Integer, ForeignKey('tbl_observations.id', 
                                        use_alter=True, name="fk_time_obs_id"))
    
    class TimeType(Base):
        __tablename__ = 'ref_tbl_time_types'
        id = Column(Integer, primary_key=True)
        desc = Column(String)
    
    
    e = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
    Base.metadata.drop_all(e)
    Base.metadata.create_all(e)
    
    s = Session(e)
    
    tt1 = TimeType(desc="some time type")
    t1, t2, t3, t4, t5 = Time(time_type=tt1, time_value=40), \
                    Time(time_type=tt1, time_value=50), \
                    Time(time_type=tt1, time_value=60),\
                    Time(time_type=tt1, time_value=70),\
                    Time(time_type=tt1, time_value=80)
    
    s.add_all([
        Observation(state=State(time=t1), times=[t1, t2]),
        Observation(state=State(time=t2), times=[t1, t3, t4]),
        Observation(state=State(time=t2), times=[t2, t3, t4, t5]),
    ])
    
    s.commit()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a

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.