in Education by
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. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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.

Related questions

0 votes
    I used the pyhton: for m in regex.findall(r"\X", 'ल्लील्ली', regex.UNICODE): for i in m: print(i, i.encode(' ... कृ','प','या','ल्ली' Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have minified all my js files using require, but require created a minified js file( main.min.js ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I have minified all my js files using require, but require created a minified js file( main.min.js ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    I'd like to ensure my RAII class is always allocated on the stack. How do I prevent a class from ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I'd like to ensure my RAII class is always allocated on the stack. How do I prevent a class from ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    How to prevent re-renders in React?...
asked Dec 5, 2020 in Technology by JackTerrance
0 votes
    If the AD B2B account is e-reference, can an admin block this? Could an admin prevent their users from being guest users in another tenant?...
asked Mar 10, 2021 in Technology by JackTerrance
0 votes
    To prevent the password from being mentioned in the sqoop import clause we can use the additional parameters A - -p B - --password-file C - both of these D - cannot be prevented...
asked Jan 13, 2021 in Technology by JackTerrance
0 votes
    How do you prevent a class from being inherited?...
asked Dec 31, 2020 in Technology by JackTerrance
0 votes
    Which of these keywords is used to prevent content of a variable from being modified? (a) final (b ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    I want an efficient way to append one string to another in Python. var1 = "foo" var2 = "bar" var3 = var1 + ... built-in method to use? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I'm working on a program that displays a list from an SQFlite table. The future for the snapshot ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 30, 2022 in Education by JackTerrance
0 votes
    Say I have a Game object with many Player objects (a baseball game). The lineup for each game has ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
...