0

I am trying to import data into existing database items. There are two fields that I want to update for each data base item: a string type field caled imagefolder and a picketype (list) field called images. The input values for both fields are strings that store file paths.

My steps are to query the database item, check for its existance, and then update its fields.

blend = Blend.query.filter_by(old_ID=row[4]).first()
if blend:
    blend.imagefolder = "/".join((row[16].split("/")[4:])[:-1])
    blend.images.append(ntpath.basename(row[16]))
    db.session.commit()

When I run the script nothing happens (the database doesn't update), and I noticed that the import script runs really quickly, too quickly.

2
  • Shouldn't it be old_ID==row[4]? (Two equal signs...) Commented Apr 13, 2019 at 21:34
  • 2
    filter() uses expressions, filter_by() uses parameters Commented Apr 14, 2019 at 1:03

2 Answers 2

3

You missed to add changed object to current session.

And try the following snippet.

blend = db.session.query(Blend).filter_by(old_ID=row[4]).first()
if blend:
    blend.imagefolder = "/".join((row[16].split("/")[4:])[:-1])
    blend.images.append(ntpath.basename(row[16]))
    db.session.add(blend)
    db.session.commit()
Sign up to request clarification or add additional context in comments.

1 Comment

I tried that and it gave an error saying it was already in the session.
0

you can't use .append with arrays. SQLAlchemy doesn't see changes this way.
You should do this instead:

blend.images = blend.images + [ntpath.basename(row[16])]

Maybe you still need this after 6 years of waiting xD

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.