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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:54:30+00:00 2026-06-11T16:54:30+00:00

I’m writing a quick one-off migration script that updates a single field in a

  • 0

I’m writing a quick one-off migration script that updates a single field in a table with half a million rows.

Since I hadn’t planned on writing out full models for the joins I’m doing to fetch the initial ~25000 rows of data, I’ve been trying to figure out how to do an UPDATE statement using a from_statement() call and using my own raw sql, but I can’t find any examples.

Along with that, SQLalchemy is throwing an error. Here’s an example of my call and error:

mydb = self.session()
mydb.query().from_statement(
    """
    UPDATE my_table
    SET settings=mysettings
    WHERE user_id=myuserid AND setting_id=123
    """).params(mysettings=new_settings, myuserid=user_id).all()

The error I get:

Traceback (most recent call last):
  File "./sample_script.py", line 111, in <module>
    main()
  File "./sample_script.py", line 108, in main
    migrate.set_migration_data()
  File "./sample_script.py", line 100, in set_migration_data
    """).params(mysettings=new_settings, myuserid=user_id).all()
  File "/usr/lib/pymodules/python2.6/sqlalchemy/orm/query.py", line 1267, in all
    return list(self)
  File "/usr/lib/pymodules/python2.6/sqlalchemy/orm/query.py", line 1361, in __iter__
    return self._execute_and_instances(context)
  File "/usr/lib/pymodules/python2.6/sqlalchemy/orm/query.py", line 1364, in _execute_and_instances
    result = self.session.execute(querycontext.statement, params=self._params, mapper=self._mapper_zero_or_none())
  File "/usr/lib/pymodules/python2.6/sqlalchemy/orm/query.py", line 251, in _mapper_zero_or_none
    if not getattr(self._entities[0], 'primary_entity', False):
IndexError: list index out of range

UPDATE

I’m using MySQL.

Per Samy’s suggestion, I tried this:

mydb.execute(
    "UPDATE mytable SET settings=:mysettings WHERE user_id=:userid AND setting_id=123",
    {'userid': user_id, 'mysettings': new_settings}
    )

This had no effect. I don’t get any errors, but the statement doesn’t seem to actually execute, as the row does not change. If I manually cut and paste the query that gets logged from the echo=True option, the row updates in the database just fine.

UPDATE – SOLVED

Samy’s suggestion was correct but the .execute() call only works on ‘engine’, not ‘session’, so this worked just fine:

self.engine.execute(
    "UPDATE mytable SET settings=:mysettings WHERE user_id=:userid AND setting_id=123",
    {'userid': user_id, 'mysettings': new_settings}
    )
  • 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-06-11T16:54:32+00:00Added an answer on June 11, 2026 at 4:54 pm

    Well this is rather strange, according to the docs, the from_statement is used for SELECT statements.

    Execute the given SELECT statement and return results.

    I could be looking at the wrong function, or it may be possible to use other type of statements, Im not really sure.

    You could just use execute since it can do any type of statement, heres a quick example.

    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker
    
    session = sessionmaker(bind = create_engine('sqlite://'), autocommit = True)()
    
    _ = session.execute('CREATE TABLE my_table (user_id int, setting_id int, settings string)')
    for id in xrange(200):
        _ = session.execute('INSERT INTO my_table (user_id, setting_id) VALUES (:user_id, :setting_id)',
            {'user_id':id, 'setting_id':id})
    
    _ = session.execute(
    """
        UPDATE my_table
        SET settings = :mysettings
        WHERE user_id = :user_id AND setting_id = 123
    """, {'user_id':123, 'mysettings':'test'})
    
    r = session.execute('SELECT * FROM my_table WHERE user_id = :user_id', {'user_id':123}).fetchall()
    print r
    
    [(123, 123, u'test')]
    

    note that this isn’t really the best way to use sqlalchemy, which was designed to create a dry environment, decoupled from a specific db backend, though you probably have your reasons for using raw sql versus its ORM.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have an autohotkey script which looks up a word in a bilingual dictionary

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.