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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:28:45+00:00 2026-05-25T00:28:45+00:00

Time for more pushing the limits of sqlalchemy. It never ceases to amaze! Background

  • 0

Time for more pushing the limits of sqlalchemy. It never ceases to amaze!

Background

I have table for devices, and a table to record physical links between them.

class Device(Base):
    __tablename__ =  "device"
    device_id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String(255), nullable=False)


class PhysicalLink(Base):
    __tablename__ =  "physical_link"
    physical_links_id = sa.Column(sa.Integer, primary_key=True)

    device_id_1 = sa.Column(sa.types.Integer, sa.ForeignKey(Device.device_id), nullable=False)
    device_port_1 = sa.Column(sa.String(255), nullable=False)

    device_id_2 = sa.Column(sa.types.Integer, sa.ForeignKey(Device.device_id), nullable=False)
    device_port_2 = sa.Column(sa.String(255), nullable=False)

    cable_number = sa.Column(sa.String(255), nullable=False)

When I dealing with the physical links for a know device, I don’t want to have to always have if statements to decide whether I should be looking at device_[id|port]_ 1 or 2, so I did:

physical_links_table = PhysicalLinks.__table__
physical_links_ua = union_all(
    select((
        physical_links_table.c.physical_links_id,
        label('this_device_id', physical_links_table.c.device_id_1),
        label('this_device_port', physical_links_table.c.device_port_1),
        label('other_device_id', physical_links_table.c.device_id_2),
        label('other_device_port', physical_links_table.c.device_port_2),
        physical_links_table.c.cable_number,
        ),),
    select((
        physical_links_table.c.physical_links_id,
        label('this_device_id', physical_links_table.c.device_id_2),
        label('this_device_port', physical_links_table.c.device_port_2),
        label('other_device_id', physical_links_table.c.device_id_1),
        label('other_device_port', physical_links_table.c.device_port_1),
        physical_links_table.c.cable_number,
        ),),
    ).alias('physical_links_ua')

class PhysicalLinksDir(object):
    pass


physical_links_dir_mapper = orm.mapper(PhysicalLinksDir, physical_links_ua)
physical_links_dir_mapper.add_property(
    'this_device', orm.relation(Device, primaryjoin=(PhysicalLinksDir.this_device_id == Device.device_id)))
physical_links_dir_mapper.add_property(
    'other_device', orm.relation(Device, primaryjoin=(PhysicalLinksDir.other_device_id == Device.device_id)))

This allows me to do:

physical_links = (db_session
    .query(PhysicalLinksDir)
    .filter(PhysicalLinksDir.this_device_id = my_device.device_id)
    .options(joinedload('other_device')))
for pl in physical_links:
    print pl.other_device

(Did I remember to tell you that I think that sqlalchmey rocks!)

Question

What do I need to do to make it possible to modify PhysicalLinksDir instance attributes, and be able to commit them back to the db?

  • 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-25T00:28:46+00:00Added an answer on May 25, 2026 at 12:28 am

    In general, you will have to be very careful with updating it the way you want,
    because those view objects PhysicalLinksDir will not always be in-sync with the
    underlying Device and PhysicalLink you might have in session/database.
    I obviously do not know your requirements, but I prefer not to have such inconsistencies when working with my model.

    Also, there is a problem with the kind of mapping you have. You would expect to have 2 rows of PhysicalLinksDir for each row of PhysicalLink (one for each side), but if you try it, you will see this is not the case. The reason for this is that the first column (physical_links_id) is considered to be a primary_key so the
    query object will discard the second one with the same value.
    In order to fix it, you need to configure the primary_key manually. Assuming there can be only one
    connection between two different Devices, the solution below will do the trick. You might need to extend it to include the port as well:

    physical_links_dir_mapper = orm.mapper(PhysicalLinksDir, physical_links_ua,
        # @note: add this
        primary_key=[physical_links_ua.c.physical_links_id, physical_links_ua.c.this_device_id],
        )
    

    DELETE: Now, to support delete, all you need to do is to add a relationship between your PLD and the actual PhysicalLink and the session.delete(my_PLD); session.commit() will also delete the PhysicalLink it represents:

    physical_links_dir_mapper.add_property(
        'physical_link', orm.relation(PhysicalLink, primaryjoin=(
                                  PhysicalLinksDir.physical_links_id == PhysicalLink.physical_links_id),
                                  foreign_keys=[PhysicalLinksDir.physical_links_id]
        ))
    

    But in fact, the deletion might work out of the box as the model is soft-linked to the physical_link table.

    INSERT: Well, this is easily done with the PhysicalLink object directly, so I would just keep it this way.

    UPDATE: You could potentially probably achieve this with Session Events, but the most simple way would be just to wrap all the attributes in a @property which would delegate the change to the proper object.

    IMPORTANT: I still think that this way of working is not really nice, because the links are not updated automatically and your in-memory UnitOfWork might be inconsistent.


    If also would be useful to understand why you think this way of working with your objects would be better? What are the use cases of this app?

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

Sidebar

Related Questions

I have a variety of time-series data stored on a more-or-less georeferenced grid, e.g.
first time use JTree. Just wondering is it possible to have more than one
I have an ASP.NET website and over time it has become more and more
Our application takes significantly more time to launch after a reboot (cold start) than
Is there an algorithm that is more time efficient than O(n^2) for detecting cycles
My program's really consuming CPU time far more than I'd like (2 displays shoots
as database transcations in our app are getting more and more time consuming, we
Can we use sleep function at applicationDidFinishLaunching to take more time to show Splash
One thing I really like about AS3 over AS2 is how much more compile-time
Is there any time in which a reference type is more efficient than 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.