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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:21:21+00:00 2026-05-11T18:21:21+00:00

I have question regarding the SQLAlchemy. How can I add into my mapped class

  • 0

I have question regarding the SQLAlchemy. How can I add into my mapped class the dictionary-like attribute, which maps the string keys into string values and which will be stored in the database (in the same or another table as original mapped object). I want this add support for arbitrary tags of my objects.

I found the following example in SQLAlchemy documentation:

from sqlalchemy.orm.collections import column_mapped_collection, attribute_mapped_collection, mapped_collection

mapper(Item, items_table, properties={
# key by column
'notes': relation(Note, collection_class=column_mapped_collection(notes_table.c.keyword)),
# or named attribute
'notes2': relation(Note, collection_class=attribute_mapped_collection('keyword')),
# or any callable
'notes3': relation(Note, collection_class=mapped_collection(lambda entity: entity.a + entity.b))
})

item = Item()
item.notes['color'] = Note('color', 'blue')

But I want the following behavior:

mapper(Item, items_table, properties={
# key by column
'notes': relation(...),
})

item = Item()
item.notes['color'] = 'blue'

It is possible in SQLAlchemy?

Thank you

  • 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-11T18:21:21+00:00Added an answer on May 11, 2026 at 6:21 pm

    The simple answer is yes.

    Just use an association proxy:

    from sqlalchemy import Column, Integer, String, Table, create_engine
    from sqlalchemy import orm, MetaData, Column, ForeignKey
    from sqlalchemy.orm import relation, mapper, sessionmaker
    from sqlalchemy.orm.collections import column_mapped_collection
    from sqlalchemy.ext.associationproxy import association_proxy
    

    Create a test environment:

    engine = create_engine('sqlite:///:memory:', echo=True)
    meta = MetaData(bind=engine)
    

    Define the tables:

    tb_items = Table('items', meta, 
            Column('id', Integer, primary_key=True), 
            Column('name', String(20)),
            Column('description', String(100)),
        )
    tb_notes = Table('notes', meta, 
            Column('id_item', Integer, ForeignKey('items.id'), primary_key=True),
            Column('name', String(20), primary_key=True),
            Column('value', String(100)),
        )
    meta.create_all()
    

    Classes (note the association_proxy in the class):

    class Note(object):
        def __init__(self, name, value):
            self.name = name
            self.value = value
    class Item(object):
        def __init__(self, name, description=''):
            self.name = name
            self.description = description
        notes = association_proxy('_notesdict', 'value', creator=Note)
    

    Mapping:

    mapper(Note, tb_notes)
    mapper(Item, tb_items, properties={
            '_notesdict': relation(Note, 
                 collection_class=column_mapped_collection(tb_notes.c.name)),
        })
    

    Then just test it:

    Session = sessionmaker(bind=engine)
    s = Session()
    
    i = Item('ball', 'A round full ball')
    i.notes['color'] = 'orange'
    i.notes['size'] = 'big'
    i.notes['data'] = 'none'
    
    s.add(i)
    s.commit()
    print i.notes
    

    That prints:

    {u'color': u'orange', u'data': u'none', u'size': u'big'}
    

    But, are those in the notes table?

    >>> print list(tb_notes.select().execute())
    [(1, u'color', u'orange'), (1, u'data', u'none'), (1, u'size', u'big')]
    

    It works!! 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 98k
  • Answers 98k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The problem is in the run method of YUV4MPEGPipeParser class.… May 11, 2026 at 7:35 pm
  • Editorial Team
    Editorial Team added an answer Go to the settings and disable the "Check for newer… May 11, 2026 at 7:35 pm
  • Editorial Team
    Editorial Team added an answer You need to look into std::allocator - the second template… May 11, 2026 at 7:34 pm

Related Questions

Does anyone have any experience that indicates what kind of performance hit a developer
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record
i have a question regarding the visitor pattern, I currently have two assemblies. My
I have a question regarding the JVM memory management (at least for the SUN's
I'm designing a Data Access layer for an C#/ASP.net application and I have a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.