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

  • Home
  • SEARCH
  • 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 9168731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:41:45+00:00 2026-06-17T15:41:45+00:00

Update: For anyone having this issue, with the very latest SQLAlchemy this behaviour has

  • 0

Update:

For anyone having this issue, with the very latest SQLAlchemy this behaviour has been fixed.

Original issue:

I am having a problem with getting association proxies to update correctly.

Using the example models here: http://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/associationproxy.html#simplifying-association-objects

But changing UserKeyword with this line:

keyword = relationship("Keyword", backref=backref("user_keywords", cascade="all, delete-orphan"))

and adding this to Keyword:

users = association_proxy('user_keywords', 'user')

So a keyword instance has a list of users.

The following works as expected:

>>> rory = User("rory")
>>> session.add(rory)
>>> chicken = Keyword('chicken')
>>> session.add(chicken)
>>> rory.keywords.append(chicken)
>>> chicken.users
[<__main__.User object at 0x1f1c0d0>]
>>> chicken.user_keywords
[<__main__.UserKeyword object at 0x1f1c450>]

But removals do strange things. Removing from the association proxy lists like so:

>>> rory.keywords.remove(chicken)

Causes an integrity error as SA tries to set one of the foreign key columns to null.

Doing this:

>>> rory.user_keywords.remove(rory.user_keywords[0])

Results in this:

>>> chicken.users
[None]

I have missed something obvious haven’t I?

  • 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-17T15:41:46+00:00Added an answer on June 17, 2026 at 3:41 pm

    UserKeyword requires that it be associated with both a Keyword and User at the same time in order to be persisted. When you associate it with a User and Keyword, but then remove it from the User.user_keywords collection, it’s still associated with the Keyword.

    >>> rory.keywords.remove(chicken)
    
    # empty as we expect
    >>> rory.user_keywords
    []   
    
    # but the other side, still populated.  UserKeyword 
    # has no User, but still has Keyword
    >>> chicken.user_keywords
    [<__main__.UserKeyword object at 0x101748d10>]
    
    # but the User on that UserKeyword is None
    >>> chicken.user_keywords[0].user is None
    True
    
    # hence accessing the "association" gives us None
    # as well
    >>> chicken.users
    [None]
    

    So if we were to flush() this right now, you’ve got a UserKeyword object ready to go but it has no User on it, so you get that NULL error. At INSERT time, the object is not considered to be an “orphan” unless it is not associated with any Keyword.user_keywords or User.user_keywords collections. Only if you were to say, del chicken.user_keywords[0] or equivalent, would you see that no INSERT is generated and the UserKeyword object is forgotten.

    If you were to flush the object to the database before removing it from “rory”, then things change. The UserKeyword is now persistent, and when you remove “chicken” from “rory.keywords”, a “delete-orphan” event fires off which does delete the UserKeyword, even though it still is associated with the Keyword object:

    rory.keywords.append(chicken)
    
    session.flush()
    
    rory.keywords.remove(chicken)
    
    session.flush()
    

    you see the SQL:

    INSERT INTO "user" (name) VALUES (%(name)s) RETURNING "user".id
    {'name': 'rory'}
    
    INSERT INTO keyword (keyword) VALUES (%(keyword)s) RETURNING keyword.id
    {'keyword': 'chicken'}
    
    INSERT INTO user_keyword (user_id, keyword_id, special_key) VALUES (%(user_id)s, %(keyword_id)s, %(special_key)s)
    {'keyword_id': 1, 'special_key': None, 'user_id': 1}
    
    DELETE FROM user_keyword WHERE user_keyword.user_id = %(user_id)s AND user_keyword.keyword_id = %(keyword_id)s
    {'keyword_id': 1, 'user_id': 1}
    

    Now a reasonable person would ask, “isn’t that inconsistent?” And at the moment I’d say, “absolutely”. I need to look into the test cases to see what the rationale is for this difference in behavior, I’ve identified in the code why it occurs in this way and I’m pretty sure this difference in how an “orphan” is considered for “pending” versus “persistent” objects is intentional, but in this particular permutation obviously produces a weird result. I might make a change in 0.8 for this if I can find one that is feasible.

    edit: http://www.sqlalchemy.org/trac/ticket/2655 summarizes the issue which I’m going to have to think about. There is a test for this behavior specifically, need to trace that back to its origin.

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

Sidebar

Related Questions

This has been discused on SOF, though, I didn't find anyone having exactly the
I'm getting a very weird issue with standard ASP.NET backend code, and the latest
Update: this issue is confirmed fixed on .NET 4.0 (I was using 3.5). I'm
Sept 2 update: This has become a very difficult puzzle to solve. Setting up
been having this problem for a while! anyway i have downloaded Maxminds GEOIP php
Has anyone tried out working with the Delete and update command of SPDataSource used
can anyone help? I have an issue with linq2sql and trying to Attach (update)
UPDATE: I have somewhat resolved the issue. Just in case if anyone runs in
I am having a bit of an issue with getting tumblr working within a
Having a very strange issue regarding postback in ASP.NET. I have page dynamically populating

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.