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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:26:48+00:00 2026-05-30T21:26:48+00:00

I just started with Flask and SQLAlchemy in flask. So I have a many-to-many

  • 0

I just started with Flask and SQLAlchemy in flask.

So I have a many-to-many relationship using the example here http://docs.sqlalchemy.org/en/latest/orm/tutorial.html
If you scrolldown to the part about Keywords and tags this is what I am working on.
So far I am able to insert new Keywords related to my Post and I am using append. Which is wrong I know. So what happens is that the next time a non unique keyword occurs in a blog post it will throw an error about Conflict with Keyword (since keywords are supposed to be unique)

I know the right way is something else, I just don’t know what. I have seen an example of
get_or_create(keyword) which basically filters by keyword and then adds it if not found. However I believe as data size grows this will also be wrong. (Several calls on every save with single insert). I love the way SQLAlchemy is doing multiple insert automatically. I wish to keep that but avoid this duplicate key issue.

Edit: found the solution, SQLAlchemy docs guide you towards error but the explanation is in there. I have added the answer.

  • 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-30T21:26:50+00:00Added an answer on May 30, 2026 at 9:26 pm

    Ok after hours of trial and error I found the solution, plus somethings I was doing wrong.

    This is how SQL alchemy works. the answer is merge.

    make a LIST of tags as Tag models, don;t matter if they exist as long as your primary key is name or something unique.

    tags = [Tag('a1'),Tag('a2')]
    

    Say you have Tag a1 already in DB but we don’t really care. All we want is to insert if related data does not exist. WHich is what SQLalchemy does.
    Now you make a Post with the LIST of ALL the tags we made. If its one only , it also is a list.

    therefore

    new_post = Post('a great new post',post_tags=tags)
    db.session.merge(new_post)
    db.session.commit()
    

    I have used Flask syntax but the idea is same. Just make sure you are not creating the Model OUTSIDE the session. More likely, you wont do it.

    This was actually simple but nowhere in the SQLAlchemy docs this example is mentioned. They use append() which is wrong. It’s only to create new Tags knowing you are not making duplicates.

    Hope it helps.

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

Sidebar

Related Questions

Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
i just started using Flash builder 4 and i have a problem. As it
I've just recently started using SQLAlchemy and am still having trouble wrapping my head
Just started using carrierwave with Rails and things have been going smoothly with one
I have just started building my first Flask app, which currently simply returns output
Just started with JQuery and I've managed to pass a parameter using POST, firebug
I am pretty new to image capturing using java. Just started working in java.
I've just stared using JQuery and SoundManger2, and I have noticed that SoundManager has
Just started using Java (for Android, if that matters) and all I want is

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.