I am using SQLALCHEMY and I can't update the table, i don't know why it doesn't work.
models.py
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Date
Base = declarative_base()
class Book(Base):
__tablename__='books'
id= Column(Integer, primary_key=True)
title=Column(String)
author = Column(String)
pages = Column(String)
published=Column(Date)
def __repr__(self):
return "<Book(title='{}', author='{}', pages={}, published={})>"\
.format(self.title, self.author, self.pages, self.published)
crud.py
from sqlalchemy import create_engine, update
from sqlalchemy.orm import sessionmaker
engine = create_engine(config.DATABASE_URI)
Session = sessionmaker(bind=engine)
s= Session()
def recreate_database():
models.Base.metadata.drop_all(engine)
models.Base.metadata.create_all(engine)
I insert the first row on the table 'books' :
book = models.Book(
title='Deep Learning',
author='Ian Goodfellow',
pages=775,
published=datetime(2016,11,18)
)
using:
recreate_database()
s.add(book)
Then I verify that row is added:
<Book(title='Deep Learning', author='Ian Goodfellow', pages=775, published=2016-11-18 00:00:00)>
And I try to update that valor changing the value of 'pages' to 900 using:
libro=models.Base.metadata.tables['books']
u=update(libro)
u=u.values({"pages": 900})
u=u.where(libro.c.title=="Deep Learning")
engine.execute(u)
s.commit()
s.close()
I don't get error but it does not update the value of pages.
I appreciate if you could tell me where is the error on my code to update the value.
Thanks !!