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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:12:36+00:00 2026-05-24T21:12:36+00:00

I have 2 Tables in SQLAlchemy (using declerative_base() ) that are joined via a

  • 0

I have 2 Tables in SQLAlchemy (using declerative_base()) that are joined via a seperate Table relationship

# no comment on the wired naming....
site_word = Table('word_site', Base.metadata,
    Column('site_id', Integer, ForeignKey('sites.site_id'), nullable = False, primary_key = True),
    Column('word_id', Integer, ForeignKey('site_words.word_id'), nullable = False, primary_key = True))

Word mapping

class Word(Base):
   # snip
   word =  Column('word', Text, nullable = False, unique = True)
   sites = relationship('Site', secondary = site_word, back_populates = 'words')

Site mapping

class Site(Base):
   # snip
   words = relationship('Word', secondary = site_word, back_populates = 'sites')

Inserting works like expected, inserting of already existing words fails of course due to unique in Word.word.

I tried using a before_insert event to check if the item already exists.

event.listen(Word, 'before_insert', some_event)

I can get the information to identify the word uniquly as expected but I don’t know how to add a new value to the join table (site_word).

I could write a trigger or procedure for the database but don’t want to move too much logic into the database (don’t know if this would be possible since the Site wouldn’t be known at that time). I could remove the constraint for the word-column but I still can’t figure out how to access the information (the other end of the join) to create an entry in the join table, but not in the Word table.

I am looking for a way to create a entry in the site_word and Site table only.

update 1:
I could attach the event to the Site but I don’t see a possibility to get the information of the to-be-inserted Site. Is there a possibility to save the Site and create the join-relation afterwards?

  • 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-24T21:12:37+00:00Added an answer on May 24, 2026 at 9:12 pm

    I managed to do it. Mark Gemmill’s answer got me thinking in the right direction.

    I need to listen to an AttributeEvent instead of an MapperEvent.

    event.listen(Site.words, 'append', test_event, retval = True)
    

    Then I can do something like:

    def test_event(target, value, initiator):
        try:
            return session.query(Word).filter(Word.word == value.word.lower()).one()
        except sqlalchemy.orm.exc.NoResultFound:
            return value 
    

    This either returns the existing word from the table or simply returns the mapped object that needs to be persisted.

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

Sidebar

Related Questions

I have a ~10M record MySQL table that I interface with using SqlAlchemy. I
In my database I have tables that define types for example Table: Publication Types
I have table in sqlalchemy 0.4 that with types.DateTime column: Column(dfield, types.DateTime, index=True) I
I'm using Turbogears2 and SQLAlchemy to develop a webapp. I have two mapped tables
I access a a postgres table using SQLAlchemy. I want a query to have
I am using the ORM Mapping in SQLAlchemy 0.6.8. I have three tables (A,
Using SQLAlchemy , I have a one to many relation with two tables -
I have a Test model/table and a TestAuditLog model/table, using SQLAlchemy and SQL Server
I have a number of classes that are mapped to tables with SQLAlchemy (non-declaratively
Does anyone has any insight on organizing sqlalchemy based projects? I have many tables

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.