NumesSanguis NumesSanguis - 1 month ago 25
Python Question

SQLAlchemy ORM update value by checking if other table value in list

I have a Kanji (Japanese characters) list which looks like:

kanji_n3 = ['政', '議', '民', '連'] # But then with 367 Kanji


and I have 2 tables:
TableKanji
and
TableMisc
.
TableMisc
has a column called 'jlpt', from which some currently have the value
2
, but this has to be updated to value
3
, if the Kanji is in
kanji_n3
.

tableclass.py

import sqlalchemy as sqla
from sqlalchemy.orm import relationship
import sqlalchemy.ext.declarative as sqld
sqla_base = sqld.declarative_base()

class TableKanji(sqla_base):
__tablename__ = 'Kanji'

id = sqla.Column(sqla.Integer, primary_key=True)
character = sqla.Column(sqla.String, nullable=False)

misc = relationship("TableMisc", back_populates='kanji')

class TableMisc(sqla_base):
__tablename__ = 'Misc'

kanji_id = sqla.Column(sqla.Integer, sqla.ForeignKey('Kanji.id'), primary_key=True)
jlpt = sqla.Column(sqla.Integer)

kanji = relationship("TableKanji", back_populates="misc")


So the query I came up with is, kanjiorigin_index.py:

import sqlalchemy as sqla
import sqlalchemy.orm as sqlo
from tableclass import TableKanji, TableMisc

kanji_n3 = ['政', '議', '民', '連'] # But then with 367 Kanji

session.query(TableMisc)\
.filter(TableMisc.jlpt == 2).filter(TableKanji.character in kanji_n3)\
.update({TableMisc.jlpt: TableMisc.jlpt + 1}, synchronize_session='fetch')


This runs succesfully, but doesn't update anything. The output:

2016-10-18 04:05:53,908 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1
2016-10-18 04:05:53,908 INFO sqlalchemy.engine.base.Engine ()
2016-10-18 04:05:53,908 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1
2016-10-18 04:05:53,909 INFO sqlalchemy.engine.base.Engine ()
2016-10-18 04:05:53,909 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2016-10-18 04:05:53,909 INFO sqlalchemy.engine.base.Engine SELECT "Misc".kanji_id AS "Misc_kanji_id"
FROM "Misc"
WHERE 0 = 1
2016-10-18 04:05:53,909 INFO sqlalchemy.engine.base.Engine ()
2016-10-18 04:05:53,910 INFO sqlalchemy.engine.base.Engine UPDATE "Misc" SET jlpt=("Misc".jlpt + ?) WHERE 0 = 1
2016-10-18 04:05:53,910 INFO sqlalchemy.engine.base.Engine (1,)
2016-10-18 04:05:53,911 INFO sqlalchemy.engine.base.Engine COMMIT


Question



How do I update
TableMisc.jlpt
where the current value is 2 and where TableKanji.character is in
kanji_n3
? Does my
in kanji_n3
statement not work like this? I also tried to add an
.outerjoin(TableKanji)
, but this results in:

sqlalchemy.exc.InvalidRequestError: Can't call Query.update() or Query.delete() when join(), outerjoin(), select_from(), or from_self() has been called

Answer

Seems like that your intention is to make an update on joined tables. Not all databases support this.

First of all you should use in_ method instead of in operator.

You can make select first and than update all selected records like this:

records = session.query(TableMisc).\
    join(TableKanji).\
    filter(TableMisc.jlpt == 2).\
    filter(TableKanji.character.in_(kanji_n3)).\
    all()

for record in records:
    record.jlpt += 1

session.commit()