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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:37:38+00:00 2026-05-31T04:37:38+00:00

This source details how to use association proxies to create views and objects with

  • 0

This source details how to use association proxies to create views and objects with values of an ORM object.

However, when I append an value that matches an existing object in the database (and said value is either unique or a primary key), it creates a conflicting object so I cannot commit.

So in my case is this only useful as a view, and I’ll need to use ORM queries to retrieve the object to be appended.

Is this my only option or can I use merge (I may only be able to do this if it’s a primary key and not a unique constraint), OR set up the constructor such that it will use an existing object in the database if it exists instead of creating a new object?

For example from the docs:

user.keywords.append('cheese inspector')

# Is translated by the association proxy into the operation:

user.kw.append(Keyword('cheese inspector'))

But I’d like to to be translated to something more like: (of course the query could fail).

keyword = session.query(Keyword).filter(Keyword.keyword == 'cheese inspector').one()
user.kw.append(keyword)

OR ideally

user.kw.append(Keyword('cheese inspector'))
session.merge() # retrieves identical object from the database, or keeps new one
session.commit() # success!

I suppose this may not even be a good idea, but it could be in certain use cases 🙂

  • 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-31T04:37:40+00:00Added an answer on May 31, 2026 at 4:37 am

    The example shown on the documentation page you link to is a composition type of relationship (in OOP terms) and as such represents the owns type of relationship rather then uses in terms of verbs. Therefore each owner would have its own copy of the same (in terms of value) keyword.

    In fact, you can use exactly the suggestion from the documentation you link to in your question to create a custom creator method and hack it to reuse existing object for given key instead of just creating a new one. In this case the sample code of the User class and creator function will look like below:

    def _keyword_find_or_create(kw):
        keyword = Keyword.query.filter_by(keyword=kw).first()
        if not(keyword):
            keyword = Keyword(keyword=kw)
            # if aufoflush=False used in the session, then uncomment below
            #session.add(keyword)
            #session.flush()
        return keyword
    
    class User(Base):
        __tablename__ = 'user'
        id = Column(Integer, primary_key=True)
        name = Column(String(64))
        kw = relationship("Keyword", secondary=lambda: userkeywords_table)
        keywords = association_proxy('kw', 'keyword', 
                creator=_keyword_find_or_create, # @note: this is the 
                )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this source: public partial class FormTest : Form { private Test test {
like this (source: photoshopessentials.com )
I'm working this source code: #include <string> #include <vector> #include <iostream> #include <istream> #include
I have this source code from 2001 that I would like to compile. It
i'm studying this source base . Basically this is an Anim server client for
I have this source code: idx=0 b=plt.psd(dOD[:,idx],Fs=self.fs,NFFT=512) B=np.zeros((2*len(self.Chan),len(b[0]))) B[idx,:]=20*log10(b[0]) c=plt.psd(dOD_filt[:,idx],Fs=self.fs,NFFT=512) C=np.zeros((2*len(self.Chan),len(b[0]))) C[idx,:]=20*log10(c[0]) for idx
I'm using a MediaElement with this source: http://origin-community.ministryofsound.com/asx/radio/mosRadio.asx When I open it with Windows
So we have a photo like this (source: vkontakte.ru ) How to detect that
So I have Image like this (source: de-viz.ru ) I want to get something
So I have Image like this (source: de-viz.ru ) I want to get something

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.