Using bulk_update_mappings when primary key column has distinct key and name

1,611 views
Skip to first unread message

Michael Williamson

unread,
Nov 10, 2016, 7:17:20 AM11/10/16
to sqlalchemy
Using bulk_update_mappings when the primary key column has a distinct key and name seems to cause an error. Specifically, running this code:

from __future__ import unicode_literals

import os

from sqlalchemy import create_engine, Column, Integer, Unicode
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, relationship


Base = declarative_base()

class Person(Base):
    __tablename__ = "person"

    id = Column(Integer, primary_key=True, name="person_id")
    name = Column(Unicode, nullable=False)

    def __repr__(self):
        return "Person(id={!r}, name={!r})".format(self.id, self.name)


engine = create_engine("sqlite:///:memory:")

Base.metadata.create_all(engine)
session = Session(engine)

person = Person(name="Bob")
session.add(person)
session.commit()
session.bulk_update_mappings(Person, [{"id": person.id, "name": "Jim"}])
session.commit()

assert session.query(Person).get(person.id).name == "Jim"


produces the error:

sqlalchemy.exc.StatementError: (sqlalchemy.exc.InvalidRequestError) A value is required for bind parameter 'person_person_id' [SQL: u'UPDATE person SET person_id=?, name=? WHERE person.person_id = ?'] [parameters: [{'person_id': 1, 'name': u'Jim'}]]

Am I missing something, or is this a bug? Or just not an intended use-case of bulk_updating_mappings? Setting the name of the column to "id" seems to work, as does setting the name column to have a distinct name.

mike bayer

unread,
Nov 10, 2016, 10:33:09 AM11/10/16
to sqlal...@googlegroups.com
this is a bug and the good news is that using the column names is not a
workaround, so nobody would be doing that either.
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to sqlalchemy+...@googlegroups.com
> <mailto:sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

mike bayer

unread,
Nov 10, 2016, 11:11:58 AM11/10/16
to sqlal...@googlegroups.com

Michael Williamson

unread,
Nov 10, 2016, 11:16:33 AM11/10/16
to sqlalchemy
Thanks, much appreciated!
Reply all
Reply to author
Forward
0 new messages