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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:20:32+00:00 2026-05-23T19:20:32+00:00

I am using Python 2.6.6 and SQLAlchemy 0.6.6 to handle a one to many

  • 0

I am using Python 2.6.6 and SQLAlchemy 0.6.6 to handle a one to many relationship in my database, and am unsure how to prevent SQLAlchemy from adding new child records in the case where similar data already exists.

Database code:

from sqlalchemy import *
from sqlalchemy.orm import backref, relationship, sessionmaker, create_session
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

# Parent table, no foreign key.
class Author(Base):
    __tablename__ = 'authors'

    id = Column(Integer, primary_key=True)
    username = Column(String)
    author_metadata = relationship('AuthorMetadata', backref='author')

# Child table, many records with same author_id.
class AuthorMetadata(Base):
    __tablename__ = 'author_metadata'

    id = Column(Integer, primary_key=True)
    author_id = Column(Integer, ForeignKey('authors.id'))
    metakey = Column(String)
    metavalue = Column(Text)

Example script:

if __name__ == '__main__':
    engine = create_engine('database_details', pool_recycle=90)
    session = create_session(bind=engine)

    author = session.query(Author).filter_by(username='Godfrey').first()
    if not author:
        author = Author()
    author.username = 'Godfrey'
    author.author_metadata = [
        AuthorMetadata(metakey='location', metavalue='New York'),
        AuthorMetadata(metakey='posts', metavalue='5')]
    session.add(author)
    session.flush()

The first time I run the example script, the following appears in the database (as expected):

dev=# select id from authors where username = 'Godfrey';
  id  
------
 5025
(1 row)

dev=# select id, author_id, metakey, metavalue from author_metadata order by id desc limit 2;
  id   | author_id | metakey  | metavalue 
-------+-----------+----------+-----------
 85090 |      5025 | posts    | 5
 85089 |      5025 | location | New York
(2 rows)

If I run the example script again though, you can see that the existing metadata record’s author ids have been set to null and new records have been inserted:

dev=# select id, author_id, metakey, metavalue from author_metadata order by id desc limit 4;
  id   | author_id | metakey  | metavalue 
-------+-----------+----------+-----------
 85092 |      5025 | posts    | 5
 85091 |      5025 | location | New York
 85090 |           | posts    | 5
 85089 |           | location | New York
(4 rows)

I don’t find this surprising, but am wondering if there is a nice way to be able to communicate to SQLAlchemy that it should only insert/modify/delete author metadata rows if the new list of metadata differs from the existing list.

  • 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-23T19:20:32+00:00Added an answer on May 23, 2026 at 7:20 pm

    You could explicitely check the contents of the list and only append new AuthorMetadata objects if they don’t exist, rather than delete the entire collection and re-create it with brand new objects. That would at least avoid discarding the previously created records.

    Your use case matches attribute_mapped_collection and association_proxy quite well, so you probably want to go with one of them.

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

Sidebar

Related Questions

I am using SqlAlchemy, a python ORM library. And I used to access database
I'm building an app in python. I'm using sqlalchemy-migrate to track my database schema.
I am developing a Python web app using sqlalchemy to communicate with mysql database.
I'm using the SQLAlchemy Python ORM in a Pylons project. I have a class
I'm using SQLalchemy for a Python project, and I want to have a tidy
Using Python, how would I go about reading in (be from a string, file
My Python / SQLAlchemy application manages a set of nodes, all derived from a
I'm looking for the simplest way of using python and SQLAlchemy to produce some
I create a table in database using sqlalchemy and now want to make a
I have a simple code to fetch users from db using sqlalchemy and return

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.