I already have a user in a database with a field named user.viewpassword which isn't displayed in __repr__. The following is the series of commands I do: >>> from flask import Flask >>> app = Flask(__name__) >>> app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:tom123@localhost/idabble' >>> from flaskext.sqlalchemy import SQLAlchemy >>> db = SQLAlchemy(app) >>> import models >>> user = models.User.query.filter_by(email='tom@google.com').first() 2011-12-04 16:54:28,599 INFO sqlalchemy.engine.base.Engine SELECT DATABASE() 2011-12-04 16:54:28,600 INFO sqlalchemy.engine.base.Engine () 2011-12-04 16:54:28,603 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'character_set%%' 2011-12-04 16:54:28,604 INFO sqlalchemy.engine.base.Engine () 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_names' 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine () 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine SHOW COLLATION 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine () 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode' 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine () 2011-12-04 16:54:28,618 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) 2011-12-04 16:54:28,620 INFO sqlalchemy.engine.base.Engine SELECT user.id AS user_id, user.email AS user_email, user.password AS user_password, user.viewpassword AS user_viewpassword FROM user WHERE user.email = %s LIMIT %s, %s 2011-12-04 16:54:28,621 INFO sqlalchemy.engine.base.Engine ('tom@google.com', 0, 1) >>> user <User('u'tom@google.com'','u'sha1$rshps9X0$ff4c48176df5c86f0042db0746479fbebacbbc66'')> >>> user.viewpassword = 'hi' >>> db.session.commit() As far as I know, this should work, but when I check my database, user.viewpassword is still null. Any ideas?
Hi! You should add your user object to the session: >> db.session.add(user) then commit: >> db.session.commit() On Mon, Dec 5, 2011 at 4:09 AM, Thomas Dziedzic <gostrc@gmail.com> wrote: > I already have a user in a database with a field named > user.viewpassword which isn't displayed in __repr__. > The following is the series of commands I do: > > >>> from flask import Flask > >>> app = Flask(__name__) > >>> app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:tom123@localhost > /idabble' > >>> from flaskext.sqlalchemy import SQLAlchemy > >>> db = SQLAlchemy(app) > >>> import models > >>> user = models.User.query.filter_by(email='tom@google.com').first() > 2011-12-04 16:54:28,599 INFO sqlalchemy.engine.base.Engine SELECT > DATABASE() > 2011-12-04 16:54:28,600 INFO sqlalchemy.engine.base.Engine () > 2011-12-04 16:54:28,603 INFO sqlalchemy.engine.base.Engine SHOW > VARIABLES LIKE 'character_set%%' > 2011-12-04 16:54:28,604 INFO sqlalchemy.engine.base.Engine () > 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine SHOW > VARIABLES LIKE 'lower_case_table_names' > 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine () > 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine SHOW COLLATION > 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine () > 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine SHOW > VARIABLES LIKE 'sql_mode' > 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine () > 2011-12-04 16:54:28,618 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) > 2011-12-04 16:54:28,620 INFO sqlalchemy.engine.base.Engine SELECT > user.id AS user_id, user.email AS user_email, user.password AS > user_password, user.viewpassword AS user_viewpassword > FROM user > WHERE user.email = %s > LIMIT %s, %s > 2011-12-04 16:54:28,621 INFO sqlalchemy.engine.base.Engine > ('tom@google.com', 0, 1) > >>> user > <User('u'tom@google.com > '','u'sha1$rshps9X0$ff4c48176df5c86f0042db0746479fbebacbbc66'')> > >>> user.viewpassword = 'hi' > >>> db.session.commit() > > As far as I know, this should work, but when I check my database, > user.viewpassword is still null. > Any ideas? >
Hi, thanks for the suggestion, but it didn't work: I put this right before db.session.commit() and got the following output >>> db.session.add(user) Traceback (most recent call last): File "<input>", line 1, in <module> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 113, in do return getattr(self.registry(), name)(*args, **kwargs) File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1161, in add self._save_or_update_state(state) File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1170, in _save_or_update_state self._save_or_update_impl(state) File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1402, in _save_or_update_impl self._update_impl(state) File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1394, in _update_impl self._attach(state) File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1428, in _attach state.session_id, self.hash_key)) InvalidRequestError: Object '<User at 0x1a75910>' is already attached to session '15348048' (this is '15347920') On Sun, Dec 4, 2011 at 5:32 PM, Andrey V. Martyanov <realduke@gmail.com> wrote: > Hi! > > You should add your user object to the session: > >>> db.session.add(user) > > then commit: > >>> db.session.commit() > > > On Mon, Dec 5, 2011 at 4:09 AM, Thomas Dziedzic <gostrc@gmail.com> wrote: >> >> I already have a user in a database with a field named >> user.viewpassword which isn't displayed in __repr__. >> The following is the series of commands I do: >> >> >>> from flask import Flask >> >>> app = Flask(__name__) >> >>> app.config['SQLALCHEMY_DATABASE_URI'] = >> >>> 'mysql://root:tom123@localhost/idabble' >> >>> from flaskext.sqlalchemy import SQLAlchemy >> >>> db = SQLAlchemy(app) >> >>> import models >> >>> user = models.User.query.filter_by(email='tom@google.com').first() >> 2011-12-04 16:54:28,599 INFO sqlalchemy.engine.base.Engine SELECT >> DATABASE() >> 2011-12-04 16:54:28,600 INFO sqlalchemy.engine.base.Engine () >> 2011-12-04 16:54:28,603 INFO sqlalchemy.engine.base.Engine SHOW >> VARIABLES LIKE 'character_set%%' >> 2011-12-04 16:54:28,604 INFO sqlalchemy.engine.base.Engine () >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine SHOW >> VARIABLES LIKE 'lower_case_table_names' >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine () >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine SHOW COLLATION >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine () >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine SHOW >> VARIABLES LIKE 'sql_mode' >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine () >> 2011-12-04 16:54:28,618 INFO sqlalchemy.engine.base.Engine BEGIN >> (implicit) >> 2011-12-04 16:54:28,620 INFO sqlalchemy.engine.base.Engine SELECT >> user.id AS user_id, user.email AS user_email, user.password AS >> user_password, user.viewpassword AS user_viewpassword >> FROM user >> WHERE user.email = %s >> LIMIT %s, %s >> 2011-12-04 16:54:28,621 INFO sqlalchemy.engine.base.Engine >> ('tom@google.com', 0, 1) >> >>> user >> >> <User('u'tom@google.com'','u'sha1$rshps9X0$ff4c48176df5c86f0042db0746479fbebacbbc66'')> >> >>> user.viewpassword = 'hi' >> >>> db.session.commit() >> >> As far as I know, this should work, but when I check my database, >> user.viewpassword is still null. >> Any ideas? > >
It's unclear to me what your model contains. If you want to separate the
main application initialization from models then you need to explicitly
import SQLAlchemy instance and use it's Model class as a base class for all
your models.
Example, based on your code:
app.py
from flask import Flask
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:tom123@localhost
/idabble'
from flaskext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
model.py
from .app import db
class User(db.Model):
pass
On Mon, Dec 5, 2011 at 4:55 AM, Thomas Dziedzic <gostrc@gmail.com> wrote:
> Hi, thanks for the suggestion, but it didn't work:
>
> I put this right before db.session.commit() and got the following output
>
> >>> db.session.add(user)
> Traceback (most recent call last):
> File "<input>", line 1, in <module>
> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py",
> line 113, in do
> return getattr(self.registry(), name)(*args, **kwargs)
> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
> line 1161, in add
> self._save_or_update_state(state)
> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
> line 1170, in _save_or_update_state
> self._save_or_update_impl(state)
> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
> line 1402, in _save_or_update_impl
> self._update_impl(state)
> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
> line 1394, in _update_impl
> self._attach(state)
> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
> line 1428, in _attach
> state.session_id, self.hash_key))
> InvalidRequestError: Object '<User at 0x1a75910>' is already attached
> to session '15348048' (this is '15347920')
>
>
> On Sun, Dec 4, 2011 at 5:32 PM, Andrey V. Martyanov <realduke@gmail.com>
> wrote:
> > Hi!
> >
> > You should add your user object to the session:
> >
> >>> db.session.add(user)
> >
> > then commit:
> >
> >>> db.session.commit()
> >
> >
> > On Mon, Dec 5, 2011 at 4:09 AM, Thomas Dziedzic <gostrc@gmail.com>
> wrote:
> >>
> >> I already have a user in a database with a field named
> >> user.viewpassword which isn't displayed in __repr__.
> >> The following is the series of commands I do:
> >>
> >> >>> from flask import Flask
> >> >>> app = Flask(__name__)
> >> >>> app.config['SQLALCHEMY_DATABASE_URI'] =
> >> >>> 'mysql://root:tom123@localhost/idabble'
> >> >>> from flaskext.sqlalchemy import SQLAlchemy
> >> >>> db = SQLAlchemy(app)
> >> >>> import models
> >> >>> user = models.User.query.filter_by(email='tom@google.com').first()
> >> 2011-12-04 16:54:28,599 INFO sqlalchemy.engine.base.Engine SELECT
> >> DATABASE()
> >> 2011-12-04 16:54:28,600 INFO sqlalchemy.engine.base.Engine ()
> >> 2011-12-04 16:54:28,603 INFO sqlalchemy.engine.base.Engine SHOW
> >> VARIABLES LIKE 'character_set%%'
> >> 2011-12-04 16:54:28,604 INFO sqlalchemy.engine.base.Engine ()
> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine SHOW
> >> VARIABLES LIKE 'lower_case_table_names'
> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine ()
> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine SHOW
> COLLATION
> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine ()
> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine SHOW
> >> VARIABLES LIKE 'sql_mode'
> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine ()
> >> 2011-12-04 16:54:28,618 INFO sqlalchemy.engine.base.Engine BEGIN
> >> (implicit)
> >> 2011-12-04 16:54:28,620 INFO sqlalchemy.engine.base.Engine SELECT
> >> user.id AS user_id, user.email AS user_email, user.password AS
> >> user_password, user.viewpassword AS user_viewpassword
> >> FROM user
> >> WHERE user.email = %s
> >> LIMIT %s, %s
> >> 2011-12-04 16:54:28,621 INFO sqlalchemy.engine.base.Engine
> >> ('tom@google.com', 0, 1)
> >> >>> user
> >>
> >> <User('u'tom@google.com
> '','u'sha1$rshps9X0$ff4c48176df5c86f0042db0746479fbebacbbc66'')>
> >> >>> user.viewpassword = 'hi'
> >> >>> db.session.commit()
> >>
> >> As far as I know, this should work, but when I check my database,
> >> user.viewpassword is still null.
> >> Any ideas?
> >
> >
>
For your specific situation the following should work:
app.py
from flask import Flask
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:tom123@localhost
/idabble'
from flaskext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
model.py
from app import db
class User(db.Model):
# Provide your model definition
pass
>>> from model import db, User
>>> user = User.query.filter_by(email='tom@google.com').first()
>>> user.viewpassword = 'hi'
>>> db.session.add(user)
>>> db.session.commit()
On Mon, Dec 5, 2011 at 6:44 AM, Andrey V. Martyanov <realduke@gmail.com>wrote:
> It's unclear to me what your model contains. If you want to separate the
> main application initialization from models then you need to explicitly
> import SQLAlchemy instance and use it's Model class as a base class for all
> your models.
>
> Example, based on your code:
>
> app.py
>
> from flask import Flask
> app = Flask(__name__)
> app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:tom123@localhost
> /idabble'
> from flaskext.sqlalchemy import SQLAlchemy
> db = SQLAlchemy(app)
>
> model.py
>
> from .app import db
>
> class User(db.Model):
> pass
>
> On Mon, Dec 5, 2011 at 4:55 AM, Thomas Dziedzic <gostrc@gmail.com> wrote:
>
>> Hi, thanks for the suggestion, but it didn't work:
>>
>> I put this right before db.session.commit() and got the following output
>>
>> >>> db.session.add(user)
>> Traceback (most recent call last):
>> File "<input>", line 1, in <module>
>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py",
>> line 113, in do
>> return getattr(self.registry(), name)(*args, **kwargs)
>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
>> line 1161, in add
>> self._save_or_update_state(state)
>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
>> line 1170, in _save_or_update_state
>> self._save_or_update_impl(state)
>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
>> line 1402, in _save_or_update_impl
>> self._update_impl(state)
>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
>> line 1394, in _update_impl
>> self._attach(state)
>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
>> line 1428, in _attach
>> state.session_id, self.hash_key))
>> InvalidRequestError: Object '<User at 0x1a75910>' is already attached
>> to session '15348048' (this is '15347920')
>>
>>
>> On Sun, Dec 4, 2011 at 5:32 PM, Andrey V. Martyanov <realduke@gmail.com>
>> wrote:
>> > Hi!
>> >
>> > You should add your user object to the session:
>> >
>> >>> db.session.add(user)
>> >
>> > then commit:
>> >
>> >>> db.session.commit()
>> >
>> >
>> > On Mon, Dec 5, 2011 at 4:09 AM, Thomas Dziedzic <gostrc@gmail.com>
>> wrote:
>> >>
>> >> I already have a user in a database with a field named
>> >> user.viewpassword which isn't displayed in __repr__.
>> >> The following is the series of commands I do:
>> >>
>> >> >>> from flask import Flask
>> >> >>> app = Flask(__name__)
>> >> >>> app.config['SQLALCHEMY_DATABASE_URI'] =
>> >> >>> 'mysql://root:tom123@localhost/idabble'
>> >> >>> from flaskext.sqlalchemy import SQLAlchemy
>> >> >>> db = SQLAlchemy(app)
>> >> >>> import models
>> >> >>> user = models.User.query.filter_by(email='tom@google.com').first()
>> >> 2011-12-04 16:54:28,599 INFO sqlalchemy.engine.base.Engine SELECT
>> >> DATABASE()
>> >> 2011-12-04 16:54:28,600 INFO sqlalchemy.engine.base.Engine ()
>> >> 2011-12-04 16:54:28,603 INFO sqlalchemy.engine.base.Engine SHOW
>> >> VARIABLES LIKE 'character_set%%'
>> >> 2011-12-04 16:54:28,604 INFO sqlalchemy.engine.base.Engine ()
>> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine SHOW
>> >> VARIABLES LIKE 'lower_case_table_names'
>> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine ()
>> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine SHOW
>> COLLATION
>> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine ()
>> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine SHOW
>> >> VARIABLES LIKE 'sql_mode'
>> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine ()
>> >> 2011-12-04 16:54:28,618 INFO sqlalchemy.engine.base.Engine BEGIN
>> >> (implicit)
>> >> 2011-12-04 16:54:28,620 INFO sqlalchemy.engine.base.Engine SELECT
>> >> user.id AS user_id, user.email AS user_email, user.password AS
>> >> user_password, user.viewpassword AS user_viewpassword
>> >> FROM user
>> >> WHERE user.email = %s
>> >> LIMIT %s, %s
>> >> 2011-12-04 16:54:28,621 INFO sqlalchemy.engine.base.Engine
>> >> ('tom@google.com', 0, 1)
>> >> >>> user
>> >>
>> >> <User('u'tom@google.com
>> '','u'sha1$rshps9X0$ff4c48176df5c86f0042db0746479fbebacbbc66'')>
>> >> >>> user.viewpassword = 'hi'
>> >> >>> db.session.commit()
>> >>
>> >> As far as I know, this should work, but when I check my database,
>> >> user.viewpassword is still null.
>> >> Any ideas?
>> >
>> >
>>
>
>
Ah, that works, but I'm still left a little confused, maybe you can explain this or point me to some resources that would explain this; in my app.py I have something like: ... db = SQLAlchemy(app) import models user = User.query.filter_by(email='tom@google.com').first() user.viewpassword = 'hi' db.session.add(user) and this doesn't work, but if I import db from models, it seems to work, so it looks like db is different in app.py and models.py I'm confused as how this works and how I could use the right db in app.py so that something like db.session.add(user) would work in app.py and not just an external file. basically I want to have the controller portion in app.py and the models seperated into models.py. Is there a clean way of doing this? Thanks for your time and answers. On Sun, Dec 4, 2011 at 7:59 PM, Andrey V. Martyanov <realduke@gmail.com> wrote: > For your specific situation the following should work: > > app.py > > from flask import Flask > app = Flask(__name__) > app.config['SQLALCHEMY_DATABASE_URI'] = > 'mysql://root:tom123@localhost/idabble' > from flaskext.sqlalchemy import SQLAlchemy > db = SQLAlchemy(app) > > model.py > > from app import db > > class User(db.Model): > # Provide your model definition > pass > >>>> from model import db, User >>>> user = User.query.filter_by(email='tom@google.com').first() >>>> user.viewpassword = 'hi' >>>> db.session.add(user) >>>> db.session.commit() > > On Mon, Dec 5, 2011 at 6:44 AM, Andrey V. Martyanov <realduke@gmail.com> > wrote: >> >> It's unclear to me what your model contains. If you want to separate the >> main application initialization from models then you need to explicitly >> import SQLAlchemy instance and use it's Model class as a base class for all >> your models. >> >> Example, based on your code: >> >> app.py >> >> from flask import Flask >> app = Flask(__name__) >> app.config['SQLALCHEMY_DATABASE_URI'] = >> 'mysql://root:tom123@localhost/idabble' >> from flaskext.sqlalchemy import SQLAlchemy >> db = SQLAlchemy(app) >> >> model.py >> >> from .app import db >> >> class User(db.Model): >> pass >> >> On Mon, Dec 5, 2011 at 4:55 AM, Thomas Dziedzic <gostrc@gmail.com> wrote: >>> >>> Hi, thanks for the suggestion, but it didn't work: >>> >>> I put this right before db.session.commit() and got the following output >>> >>> >>> db.session.add(user) >>> Traceback (most recent call last): >>> File "<input>", line 1, in <module> >>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", >>> line 113, in do >>> return getattr(self.registry(), name)(*args, **kwargs) >>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>> line 1161, in add >>> self._save_or_update_state(state) >>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>> line 1170, in _save_or_update_state >>> self._save_or_update_impl(state) >>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>> line 1402, in _save_or_update_impl >>> self._update_impl(state) >>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>> line 1394, in _update_impl >>> self._attach(state) >>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>> line 1428, in _attach >>> state.session_id, self.hash_key)) >>> InvalidRequestError: Object '<User at 0x1a75910>' is already attached >>> to session '15348048' (this is '15347920') >>> >>> >>> On Sun, Dec 4, 2011 at 5:32 PM, Andrey V. Martyanov <realduke@gmail.com> >>> wrote: >>> > Hi! >>> > >>> > You should add your user object to the session: >>> > >>> >>> db.session.add(user) >>> > >>> > then commit: >>> > >>> >>> db.session.commit() >>> > >>> > >>> > On Mon, Dec 5, 2011 at 4:09 AM, Thomas Dziedzic <gostrc@gmail.com> >>> > wrote: >>> >> >>> >> I already have a user in a database with a field named >>> >> user.viewpassword which isn't displayed in __repr__. >>> >> The following is the series of commands I do: >>> >> >>> >> >>> from flask import Flask >>> >> >>> app = Flask(__name__) >>> >> >>> app.config['SQLALCHEMY_DATABASE_URI'] = >>> >> >>> 'mysql://root:tom123@localhost/idabble' >>> >> >>> from flaskext.sqlalchemy import SQLAlchemy >>> >> >>> db = SQLAlchemy(app) >>> >> >>> import models >>> >> >>> user = models.User.query.filter_by(email='tom@google.com').first() >>> >> 2011-12-04 16:54:28,599 INFO sqlalchemy.engine.base.Engine SELECT >>> >> DATABASE() >>> >> 2011-12-04 16:54:28,600 INFO sqlalchemy.engine.base.Engine () >>> >> 2011-12-04 16:54:28,603 INFO sqlalchemy.engine.base.Engine SHOW >>> >> VARIABLES LIKE 'character_set%%' >>> >> 2011-12-04 16:54:28,604 INFO sqlalchemy.engine.base.Engine () >>> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine SHOW >>> >> VARIABLES LIKE 'lower_case_table_names' >>> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine () >>> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine SHOW >>> >> COLLATION >>> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine () >>> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine SHOW >>> >> VARIABLES LIKE 'sql_mode' >>> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine () >>> >> 2011-12-04 16:54:28,618 INFO sqlalchemy.engine.base.Engine BEGIN >>> >> (implicit) >>> >> 2011-12-04 16:54:28,620 INFO sqlalchemy.engine.base.Engine SELECT >>> >> user.id AS user_id, user.email AS user_email, user.password AS >>> >> user_password, user.viewpassword AS user_viewpassword >>> >> FROM user >>> >> WHERE user.email = %s >>> >> LIMIT %s, %s >>> >> 2011-12-04 16:54:28,621 INFO sqlalchemy.engine.base.Engine >>> >> ('tom@google.com', 0, 1) >>> >> >>> user >>> >> >>> >> >>> >> <User('u'tom@google.com'','u'sha1$rshps9X0$ff4c48176df5c86f0042db0746479fbebacbbc66'')> >>> >> >>> user.viewpassword = 'hi' >>> >> >>> db.session.commit() >>> >> >>> >> As far as I know, this should work, but when I check my database, >>> >> user.viewpassword is still null. >>> >> Any ideas? >>> > >>> > >> >> >
Correct me if I'm wrong but I don't think you need to add user to the session when you are modifying existing records. I believe its already added. On Mon, Dec 5, 2011 at 11:05 AM, Thomas Dziedzic <gostrc@gmail.com> wrote: > Ah, that works, > but I'm still left a little confused, maybe you can explain this or > point me to some resources that would explain this; > > in my app.py I have something like: > ... > db = SQLAlchemy(app) > import models > user = User.query.filter_by(email='tom@google.com').first() > user.viewpassword = 'hi' > db.session.add(user) > > and this doesn't work, > > but if I import db from models, it seems to work, > so it looks like db is different in app.py and models.py > I'm confused as how this works and how I could use the right db in > app.py so that something like db.session.add(user) would work in > app.py and not just an external file. > > basically I want to have the controller portion in app.py and the > models seperated into models.py. Is there a clean way of doing this? > > Thanks for your time and answers. > > On Sun, Dec 4, 2011 at 7:59 PM, Andrey V. Martyanov <realduke@gmail.com> wrote: >> For your specific situation the following should work: >> >> app.py >> >> from flask import Flask >> app = Flask(__name__) >> app.config['SQLALCHEMY_DATABASE_URI'] = >> 'mysql://root:tom123@localhost/idabble' >> from flaskext.sqlalchemy import SQLAlchemy >> db = SQLAlchemy(app) >> >> model.py >> >> from app import db >> >> class User(db.Model): >> # Provide your model definition >> pass >> >>>>> from model import db, User >>>>> user = User.query.filter_by(email='tom@google.com').first() >>>>> user.viewpassword = 'hi' >>>>> db.session.add(user) >>>>> db.session.commit() >> >> On Mon, Dec 5, 2011 at 6:44 AM, Andrey V. Martyanov <realduke@gmail.com> >> wrote: >>> >>> It's unclear to me what your model contains. If you want to separate the >>> main application initialization from models then you need to explicitly >>> import SQLAlchemy instance and use it's Model class as a base class for all >>> your models. >>> >>> Example, based on your code: >>> >>> app.py >>> >>> from flask import Flask >>> app = Flask(__name__) >>> app.config['SQLALCHEMY_DATABASE_URI'] = >>> 'mysql://root:tom123@localhost/idabble' >>> from flaskext.sqlalchemy import SQLAlchemy >>> db = SQLAlchemy(app) >>> >>> model.py >>> >>> from .app import db >>> >>> class User(db.Model): >>> pass >>> >>> On Mon, Dec 5, 2011 at 4:55 AM, Thomas Dziedzic <gostrc@gmail.com> wrote: >>>> >>>> Hi, thanks for the suggestion, but it didn't work: >>>> >>>> I put this right before db.session.commit() and got the following output >>>> >>>> >>> db.session.add(user) >>>> Traceback (most recent call last): >>>> File "<input>", line 1, in <module> >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", >>>> line 113, in do >>>> return getattr(self.registry(), name)(*args, **kwargs) >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>>> line 1161, in add >>>> self._save_or_update_state(state) >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>>> line 1170, in _save_or_update_state >>>> self._save_or_update_impl(state) >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>>> line 1402, in _save_or_update_impl >>>> self._update_impl(state) >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>>> line 1394, in _update_impl >>>> self._attach(state) >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>>> line 1428, in _attach >>>> state.session_id, self.hash_key)) >>>> InvalidRequestError: Object '<User at 0x1a75910>' is already attached >>>> to session '15348048' (this is '15347920') >>>> >>>> >>>> On Sun, Dec 4, 2011 at 5:32 PM, Andrey V. Martyanov <realduke@gmail.com> >>>> wrote: >>>> > Hi! >>>> > >>>> > You should add your user object to the session: >>>> > >>>> >>> db.session.add(user) >>>> > >>>> > then commit: >>>> > >>>> >>> db.session.commit() >>>> > >>>> > >>>> > On Mon, Dec 5, 2011 at 4:09 AM, Thomas Dziedzic <gostrc@gmail.com> >>>> > wrote: >>>> >> >>>> >> I already have a user in a database with a field named >>>> >> user.viewpassword which isn't displayed in __repr__. >>>> >> The following is the series of commands I do: >>>> >> >>>> >> >>> from flask import Flask >>>> >> >>> app = Flask(__name__) >>>> >> >>> app.config['SQLALCHEMY_DATABASE_URI'] = >>>> >> >>> 'mysql://root:tom123@localhost/idabble' >>>> >> >>> from flaskext.sqlalchemy import SQLAlchemy >>>> >> >>> db = SQLAlchemy(app) >>>> >> >>> import models >>>> >> >>> user = models.User.query.filter_by(email='tom@google.com').first() >>>> >> 2011-12-04 16:54:28,599 INFO sqlalchemy.engine.base.Engine SELECT >>>> >> DATABASE() >>>> >> 2011-12-04 16:54:28,600 INFO sqlalchemy.engine.base.Engine () >>>> >> 2011-12-04 16:54:28,603 INFO sqlalchemy.engine.base.Engine SHOW >>>> >> VARIABLES LIKE 'character_set%%' >>>> >> 2011-12-04 16:54:28,604 INFO sqlalchemy.engine.base.Engine () >>>> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine SHOW >>>> >> VARIABLES LIKE 'lower_case_table_names' >>>> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine () >>>> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine SHOW >>>> >> COLLATION >>>> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine () >>>> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine SHOW >>>> >> VARIABLES LIKE 'sql_mode' >>>> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine () >>>> >> 2011-12-04 16:54:28,618 INFO sqlalchemy.engine.base.Engine BEGIN >>>> >> (implicit) >>>> >> 2011-12-04 16:54:28,620 INFO sqlalchemy.engine.base.Engine SELECT >>>> >> user.id AS user_id, user.email AS user_email, user.password AS >>>> >> user_password, user.viewpassword AS user_viewpassword >>>> >> FROM user >>>> >> WHERE user.email = %s >>>> >> LIMIT %s, %s >>>> >> 2011-12-04 16:54:28,621 INFO sqlalchemy.engine.base.Engine >>>> >> ('tom@google.com', 0, 1) >>>> >> >>> user >>>> >> >>>> >> >>>> >> <User('u'tom@google.com'','u'sha1$rshps9X0$ff4c48176df5c86f0042db0746479fbebacbbc66'')> >>>> >> >>> user.viewpassword = 'hi' >>>> >> >>> db.session.commit() >>>> >> >>>> >> As far as I know, this should work, but when I check my database, >>>> >> user.viewpassword is still null. >>>> >> Any ideas? >>>> > >>>> > >>> >>> >>
The concepts behind SQLAlchemy is a bit hard for newbies. Read about SQLAlchemy sessions http://www.sqlalchemy.org/docs/orm/session.html, especially the part about thread-locals and contexts http://www.sqlalchemy.org/docs/orm/session.html#unitofwork-contextual. Flask-SQLAlchemy extension is implemented in one file https://github.com/mitsuhiko/flask-sqlalchemy/blob/master/flaskext/sqlalchemy.py. SQLAlchemy in Flask is a worth reading too http://flask.pocoo.org/docs/patterns/sqlalchemy/. Much easier way is to use something like https://github.com/imlucas/flask-tool or read the source of projects powered by Flask. Newsmeme is a good example http://bitbucket.org/danjac/newsmeme. In general, placing your controllers in app.py is not a good idea. Good luck! On Mon, Dec 5, 2011 at 11:45 AM, Adam Patterson <fakeempire@gmail.com>wrote: > Correct me if I'm wrong but I don't think you need to add user to the > session when you are modifying existing records. I believe its already > added. > > On Mon, Dec 5, 2011 at 11:05 AM, Thomas Dziedzic <gostrc@gmail.com> wrote: > > Ah, that works, > > but I'm still left a little confused, maybe you can explain this or > > point me to some resources that would explain this; > > > > in my app.py I have something like: > > ... > > db = SQLAlchemy(app) > > import models > > user = User.query.filter_by(email='tom@google.com').first() > > user.viewpassword = 'hi' > > db.session.add(user) > > > > and this doesn't work, > > > > but if I import db from models, it seems to work, > > so it looks like db is different in app.py and models.py > > I'm confused as how this works and how I could use the right db in > > app.py so that something like db.session.add(user) would work in > > app.py and not just an external file. > > > > basically I want to have the controller portion in app.py and the > > models seperated into models.py. Is there a clean way of doing this? > > > > Thanks for your time and answers. > > > > On Sun, Dec 4, 2011 at 7:59 PM, Andrey V. Martyanov <realduke@gmail.com> > wrote: > >> For your specific situation the following should work: > >> > >> app.py > >> > >> from flask import Flask > >> app = Flask(__name__) > >> app.config['SQLALCHEMY_DATABASE_URI'] = > >> 'mysql://root:tom123@localhost/idabble' > >> from flaskext.sqlalchemy import SQLAlchemy > >> db = SQLAlchemy(app) > >> > >> model.py > >> > >> from app import db > >> > >> class User(db.Model): > >> # Provide your model definition > >> pass > >> > >>>>> from model import db, User > >>>>> user = User.query.filter_by(email='tom@google.com').first() > >>>>> user.viewpassword = 'hi' > >>>>> db.session.add(user) > >>>>> db.session.commit() > >> > >> On Mon, Dec 5, 2011 at 6:44 AM, Andrey V. Martyanov <realduke@gmail.com > > > >> wrote: > >>> > >>> It's unclear to me what your model contains. If you want to separate > the > >>> main application initialization from models then you need to > explicitly > >>> import SQLAlchemy instance and use it's Model class as a base class > for all > >>> your models. > >>> > >>> Example, based on your code: > >>> > >>> app.py > >>> > >>> from flask import Flask > >>> app = Flask(__name__) > >>> app.config['SQLALCHEMY_DATABASE_URI'] = > >>> 'mysql://root:tom123@localhost/idabble' > >>> from flaskext.sqlalchemy import SQLAlchemy > >>> db = SQLAlchemy(app) > >>> > >>> model.py > >>> > >>> from .app import db > >>> > >>> class User(db.Model): > >>> pass > >>> > >>> On Mon, Dec 5, 2011 at 4:55 AM, Thomas Dziedzic <gostrc@gmail.com> > wrote: > >>>> > >>>> Hi, thanks for the suggestion, but it didn't work: > >>>> > >>>> I put this right before db.session.commit() and got the following > output > >>>> > >>>> >>> db.session.add(user) > >>>> Traceback (most recent call last): > >>>> File "<input>", line 1, in <module> > >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", > >>>> line 113, in do > >>>> return getattr(self.registry(), name)(*args, **kwargs) > >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", > >>>> line 1161, in add > >>>> self._save_or_update_state(state) > >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", > >>>> line 1170, in _save_or_update_state > >>>> self._save_or_update_impl(state) > >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", > >>>> line 1402, in _save_or_update_impl > >>>> self._update_impl(state) > >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", > >>>> line 1394, in _update_impl > >>>> self._attach(state) > >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", > >>>> line 1428, in _attach > >>>> state.session_id, self.hash_key)) > >>>> InvalidRequestError: Object '<User at 0x1a75910>' is already attached > >>>> to session '15348048' (this is '15347920') > >>>> > >>>> > >>>> On Sun, Dec 4, 2011 at 5:32 PM, Andrey V. Martyanov < > realduke@gmail.com> > >>>> wrote: > >>>> > Hi! > >>>> > > >>>> > You should add your user object to the session: > >>>> > > >>>> >>> db.session.add(user) > >>>> > > >>>> > then commit: > >>>> > > >>>> >>> db.session.commit() > >>>> > > >>>> > > >>>> > On Mon, Dec 5, 2011 at 4:09 AM, Thomas Dziedzic <gostrc@gmail.com> > >>>> > wrote: > >>>> >> > >>>> >> I already have a user in a database with a field named > >>>> >> user.viewpassword which isn't displayed in __repr__. > >>>> >> The following is the series of commands I do: > >>>> >> > >>>> >> >>> from flask import Flask > >>>> >> >>> app = Flask(__name__) > >>>> >> >>> app.config['SQLALCHEMY_DATABASE_URI'] = > >>>> >> >>> 'mysql://root:tom123@localhost/idabble' > >>>> >> >>> from flaskext.sqlalchemy import SQLAlchemy > >>>> >> >>> db = SQLAlchemy(app) > >>>> >> >>> import models > >>>> >> >>> user = models.User.query.filter_by(email='tom@google.com > ').first() > >>>> >> 2011-12-04 16:54:28,599 INFO sqlalchemy.engine.base.Engine SELECT > >>>> >> DATABASE() > >>>> >> 2011-12-04 16:54:28,600 INFO sqlalchemy.engine.base.Engine () > >>>> >> 2011-12-04 16:54:28,603 INFO sqlalchemy.engine.base.Engine SHOW > >>>> >> VARIABLES LIKE 'character_set%%' > >>>> >> 2011-12-04 16:54:28,604 INFO sqlalchemy.engine.base.Engine () > >>>> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine SHOW > >>>> >> VARIABLES LIKE 'lower_case_table_names' > >>>> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine () > >>>> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine SHOW > >>>> >> COLLATION > >>>> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine () > >>>> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine SHOW > >>>> >> VARIABLES LIKE 'sql_mode' > >>>> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine () > >>>> >> 2011-12-04 16:54:28,618 INFO sqlalchemy.engine.base.Engine BEGIN > >>>> >> (implicit) > >>>> >> 2011-12-04 16:54:28,620 INFO sqlalchemy.engine.base.Engine SELECT > >>>> >> user.id AS user_id, user.email AS user_email, user.password AS > >>>> >> user_password, user.viewpassword AS user_viewpassword > >>>> >> FROM user > >>>> >> WHERE user.email = %s > >>>> >> LIMIT %s, %s > >>>> >> 2011-12-04 16:54:28,621 INFO sqlalchemy.engine.base.Engine > >>>> >> ('tom@google.com', 0, 1) > >>>> >> >>> user > >>>> >> > >>>> >> > >>>> >> <User('u'tom@google.com > '','u'sha1$rshps9X0$ff4c48176df5c86f0042db0746479fbebacbbc66'')> > >>>> >> >>> user.viewpassword = 'hi' > >>>> >> >>> db.session.commit() > >>>> >> > >>>> >> As far as I know, this should work, but when I check my database, > >>>> >> user.viewpassword is still null. > >>>> >> Any ideas? > >>>> > > >>>> > > >>> > >>> > >> >
Adam, added by whom? You should explicitly add an object to the session. On Mon, Dec 5, 2011 at 11:45 AM, Adam Patterson <fakeempire@gmail.com>wrote: > >> Correct me if I'm wrong but I don't think you need to add user to the >> session when you are modifying existing records. I believe its already >> added. >> >> On Mon, Dec 5, 2011 at 11:05 AM, Thomas Dziedzic <gostrc@gmail.com> >> wrote: >> > Ah, that works, >> > but I'm still left a little confused, maybe you can explain this or >> > point me to some resources that would explain this; >> > >> > in my app.py I have something like: >> > ... >> > db = SQLAlchemy(app) >> > import models >> > user = User.query.filter_by(email='tom@google.com').first() >> > user.viewpassword = 'hi' >> > db.session.add(user) >> > >> > and this doesn't work, >> > >> > but if I import db from models, it seems to work, >> > so it looks like db is different in app.py and models.py >> > I'm confused as how this works and how I could use the right db in >> > app.py so that something like db.session.add(user) would work in >> > app.py and not just an external file. >> > >> > basically I want to have the controller portion in app.py and the >> > models seperated into models.py. Is there a clean way of doing this? >> > >> > Thanks for your time and answers. >> > >> > On Sun, Dec 4, 2011 at 7:59 PM, Andrey V. Martyanov <realduke@gmail.com> >> wrote: >> >> For your specific situation the following should work: >> >> >> >> app.py >> >> >> >> from flask import Flask >> >> app = Flask(__name__) >> >> app.config['SQLALCHEMY_DATABASE_URI'] = >> >> 'mysql://root:tom123@localhost/idabble' >> >> from flaskext.sqlalchemy import SQLAlchemy >> >> db = SQLAlchemy(app) >> >> >> >> model.py >> >> >> >> from app import db >> >> >> >> class User(db.Model): >> >> # Provide your model definition >> >> pass >> >> >> >>>>> from model import db, User >> >>>>> user = User.query.filter_by(email='tom@google.com').first() >> >>>>> user.viewpassword = 'hi' >> >>>>> db.session.add(user) >> >>>>> db.session.commit() >> >> >> >> On Mon, Dec 5, 2011 at 6:44 AM, Andrey V. Martyanov < >> realduke@gmail.com> >> >> wrote: >> >>> >> >>> It's unclear to me what your model contains. If you want to separate >> the >> >>> main application initialization from models then you need to >> explicitly >> >>> import SQLAlchemy instance and use it's Model class as a base class >> for all >> >>> your models. >> >>> >> >>> Example, based on your code: >> >>> >> >>> app.py >> >>> >> >>> from flask import Flask >> >>> app = Flask(__name__) >> >>> app.config['SQLALCHEMY_DATABASE_URI'] = >> >>> 'mysql://root:tom123@localhost/idabble' >> >>> from flaskext.sqlalchemy import SQLAlchemy >> >>> db = SQLAlchemy(app) >> >>> >> >>> model.py >> >>> >> >>> from .app import db >> >>> >> >>> class User(db.Model): >> >>> pass >> >>> >> >>> On Mon, Dec 5, 2011 at 4:55 AM, Thomas Dziedzic <gostrc@gmail.com> >> wrote: >> >>>> >> >>>> Hi, thanks for the suggestion, but it didn't work: >> >>>> >> >>>> I put this right before db.session.commit() and got the following >> output >> >>>> >> >>>> >>> db.session.add(user) >> >>>> Traceback (most recent call last): >> >>>> File "<input>", line 1, in <module> >> >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", >> >>>> line 113, in do >> >>>> return getattr(self.registry(), name)(*args, **kwargs) >> >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >> >>>> line 1161, in add >> >>>> self._save_or_update_state(state) >> >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >> >>>> line 1170, in _save_or_update_state >> >>>> self._save_or_update_impl(state) >> >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >> >>>> line 1402, in _save_or_update_impl >> >>>> self._update_impl(state) >> >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >> >>>> line 1394, in _update_impl >> >>>> self._attach(state) >> >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >> >>>> line 1428, in _attach >> >>>> state.session_id, self.hash_key)) >> >>>> InvalidRequestError: Object '<User at 0x1a75910>' is already attached >> >>>> to session '15348048' (this is '15347920') >> >>>> >> >>>> >> >>>> On Sun, Dec 4, 2011 at 5:32 PM, Andrey V. Martyanov < >> realduke@gmail.com> >> >>>> wrote: >> >>>> > Hi! >> >>>> > >> >>>> > You should add your user object to the session: >> >>>> > >> >>>> >>> db.session.add(user) >> >>>> > >> >>>> > then commit: >> >>>> > >> >>>> >>> db.session.commit() >> >>>> > >> >>>> > >> >>>> > On Mon, Dec 5, 2011 at 4:09 AM, Thomas Dziedzic <gostrc@gmail.com> >> >>>> > wrote: >> >>>> >> >> >>>> >> I already have a user in a database with a field named >> >>>> >> user.viewpassword which isn't displayed in __repr__. >> >>>> >> The following is the series of commands I do: >> >>>> >> >> >>>> >> >>> from flask import Flask >> >>>> >> >>> app = Flask(__name__) >> >>>> >> >>> app.config['SQLALCHEMY_DATABASE_URI'] = >> >>>> >> >>> 'mysql://root:tom123@localhost/idabble' >> >>>> >> >>> from flaskext.sqlalchemy import SQLAlchemy >> >>>> >> >>> db = SQLAlchemy(app) >> >>>> >> >>> import models >> >>>> >> >>> user = models.User.query.filter_by(email='tom@google.com >> ').first() >> >>>> >> 2011-12-04 16:54:28,599 INFO sqlalchemy.engine.base.Engine SELECT >> >>>> >> DATABASE() >> >>>> >> 2011-12-04 16:54:28,600 INFO sqlalchemy.engine.base.Engine () >> >>>> >> 2011-12-04 16:54:28,603 INFO sqlalchemy.engine.base.Engine SHOW >> >>>> >> VARIABLES LIKE 'character_set%%' >> >>>> >> 2011-12-04 16:54:28,604 INFO sqlalchemy.engine.base.Engine () >> >>>> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine SHOW >> >>>> >> VARIABLES LIKE 'lower_case_table_names' >> >>>> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine () >> >>>> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine SHOW >> >>>> >> COLLATION >> >>>> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine () >> >>>> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine SHOW >> >>>> >> VARIABLES LIKE 'sql_mode' >> >>>> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine () >> >>>> >> 2011-12-04 16:54:28,618 INFO sqlalchemy.engine.base.Engine BEGIN >> >>>> >> (implicit) >> >>>> >> 2011-12-04 16:54:28,620 INFO sqlalchemy.engine.base.Engine SELECT >> >>>> >> user.id AS user_id, user.email AS user_email, user.password AS >> >>>> >> user_password, user.viewpassword AS user_viewpassword >> >>>> >> FROM user >> >>>> >> WHERE user.email = %s >> >>>> >> LIMIT %s, %s >> >>>> >> 2011-12-04 16:54:28,621 INFO sqlalchemy.engine.base.Engine >> >>>> >> ('tom@google.com', 0, 1) >> >>>> >> >>> user >> >>>> >> >> >>>> >> >> >>>> >> <User('u'tom@google.com >> '','u'sha1$rshps9X0$ff4c48176df5c86f0042db0746479fbebacbbc66'')> >> >>>> >> >>> user.viewpassword = 'hi' >> >>>> >> >>> db.session.commit() >> >>>> >> >> >>>> >> As far as I know, this should work, but when I check my database, >> >>>> >> user.viewpassword is still null. >> >>>> >> Any ideas? >> >>>> > >> >>>> > >> >>> >> >>> >> >> >> > >
This is a quote from Armin: "Unnecessary for modifications. The object is automatically part of the session if it was retrieved via Object.query." Here is the context: http://flask.pocoo.org/mailinglist/archive/2011/10/18/updating-an-existing-record-with-sqlalchemy/ You can see Armin's response about 5 messages down. On Mon, Dec 5, 2011 at 6:16 PM, Andrey V. Martyanov <realduke@gmail.com> wrote: > Adam, added by whom? You should explicitly add an object to the session. > > >> On Mon, Dec 5, 2011 at 11:45 AM, Adam Patterson <fakeempire@gmail.com> >> wrote: >>> >>> Correct me if I'm wrong but I don't think you need to add user to the >>> session when you are modifying existing records. I believe its already >>> added. >>> >>> On Mon, Dec 5, 2011 at 11:05 AM, Thomas Dziedzic <gostrc@gmail.com> >>> wrote: >>> > Ah, that works, >>> > but I'm still left a little confused, maybe you can explain this or >>> > point me to some resources that would explain this; >>> > >>> > in my app.py I have something like: >>> > ... >>> > db = SQLAlchemy(app) >>> > import models >>> > user = User.query.filter_by(email='tom@google.com').first() >>> > user.viewpassword = 'hi' >>> > db.session.add(user) >>> > >>> > and this doesn't work, >>> > >>> > but if I import db from models, it seems to work, >>> > so it looks like db is different in app.py and models.py >>> > I'm confused as how this works and how I could use the right db in >>> > app.py so that something like db.session.add(user) would work in >>> > app.py and not just an external file. >>> > >>> > basically I want to have the controller portion in app.py and the >>> > models seperated into models.py. Is there a clean way of doing this? >>> > >>> > Thanks for your time and answers. >>> > >>> > On Sun, Dec 4, 2011 at 7:59 PM, Andrey V. Martyanov >>> > <realduke@gmail.com> wrote: >>> >> For your specific situation the following should work: >>> >> >>> >> app.py >>> >> >>> >> from flask import Flask >>> >> app = Flask(__name__) >>> >> app.config['SQLALCHEMY_DATABASE_URI'] = >>> >> 'mysql://root:tom123@localhost/idabble' >>> >> from flaskext.sqlalchemy import SQLAlchemy >>> >> db = SQLAlchemy(app) >>> >> >>> >> model.py >>> >> >>> >> from app import db >>> >> >>> >> class User(db.Model): >>> >> # Provide your model definition >>> >> pass >>> >> >>> >>>>> from model import db, User >>> >>>>> user = User.query.filter_by(email='tom@google.com').first() >>> >>>>> user.viewpassword = 'hi' >>> >>>>> db.session.add(user) >>> >>>>> db.session.commit() >>> >> >>> >> On Mon, Dec 5, 2011 at 6:44 AM, Andrey V. Martyanov >>> >> <realduke@gmail.com> >>> >> wrote: >>> >>> >>> >>> It's unclear to me what your model contains. If you want to separate >>> >>> the >>> >>> main application initialization from models then you need to >>> >>> explicitly >>> >>> import SQLAlchemy instance and use it's Model class as a base class >>> >>> for all >>> >>> your models. >>> >>> >>> >>> Example, based on your code: >>> >>> >>> >>> app.py >>> >>> >>> >>> from flask import Flask >>> >>> app = Flask(__name__) >>> >>> app.config['SQLALCHEMY_DATABASE_URI'] = >>> >>> 'mysql://root:tom123@localhost/idabble' >>> >>> from flaskext.sqlalchemy import SQLAlchemy >>> >>> db = SQLAlchemy(app) >>> >>> >>> >>> model.py >>> >>> >>> >>> from .app import db >>> >>> >>> >>> class User(db.Model): >>> >>> pass >>> >>> >>> >>> On Mon, Dec 5, 2011 at 4:55 AM, Thomas Dziedzic <gostrc@gmail.com> >>> >>> wrote: >>> >>>> >>> >>>> Hi, thanks for the suggestion, but it didn't work: >>> >>>> >>> >>>> I put this right before db.session.commit() and got the following >>> >>>> output >>> >>>> >>> >>>> >>> db.session.add(user) >>> >>>> Traceback (most recent call last): >>> >>>> File "<input>", line 1, in <module> >>> >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", >>> >>>> line 113, in do >>> >>>> return getattr(self.registry(), name)(*args, **kwargs) >>> >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>> >>>> line 1161, in add >>> >>>> self._save_or_update_state(state) >>> >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>> >>>> line 1170, in _save_or_update_state >>> >>>> self._save_or_update_impl(state) >>> >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>> >>>> line 1402, in _save_or_update_impl >>> >>>> self._update_impl(state) >>> >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>> >>>> line 1394, in _update_impl >>> >>>> self._attach(state) >>> >>>> File "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", >>> >>>> line 1428, in _attach >>> >>>> state.session_id, self.hash_key)) >>> >>>> InvalidRequestError: Object '<User at 0x1a75910>' is already >>> >>>> attached >>> >>>> to session '15348048' (this is '15347920') >>> >>>> >>> >>>> >>> >>>> On Sun, Dec 4, 2011 at 5:32 PM, Andrey V. Martyanov >>> >>>> <realduke@gmail.com> >>> >>>> wrote: >>> >>>> > Hi! >>> >>>> > >>> >>>> > You should add your user object to the session: >>> >>>> > >>> >>>> >>> db.session.add(user) >>> >>>> > >>> >>>> > then commit: >>> >>>> > >>> >>>> >>> db.session.commit() >>> >>>> > >>> >>>> > >>> >>>> > On Mon, Dec 5, 2011 at 4:09 AM, Thomas Dziedzic <gostrc@gmail.com> >>> >>>> > wrote: >>> >>>> >> >>> >>>> >> I already have a user in a database with a field named >>> >>>> >> user.viewpassword which isn't displayed in __repr__. >>> >>>> >> The following is the series of commands I do: >>> >>>> >> >>> >>>> >> >>> from flask import Flask >>> >>>> >> >>> app = Flask(__name__) >>> >>>> >> >>> app.config['SQLALCHEMY_DATABASE_URI'] = >>> >>>> >> >>> 'mysql://root:tom123@localhost/idabble' >>> >>>> >> >>> from flaskext.sqlalchemy import SQLAlchemy >>> >>>> >> >>> db = SQLAlchemy(app) >>> >>>> >> >>> import models >>> >>>> >> >>> user = >>> >>>> >> >>> models.User.query.filter_by(email='tom@google.com').first() >>> >>>> >> 2011-12-04 16:54:28,599 INFO sqlalchemy.engine.base.Engine SELECT >>> >>>> >> DATABASE() >>> >>>> >> 2011-12-04 16:54:28,600 INFO sqlalchemy.engine.base.Engine () >>> >>>> >> 2011-12-04 16:54:28,603 INFO sqlalchemy.engine.base.Engine SHOW >>> >>>> >> VARIABLES LIKE 'character_set%%' >>> >>>> >> 2011-12-04 16:54:28,604 INFO sqlalchemy.engine.base.Engine () >>> >>>> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine SHOW >>> >>>> >> VARIABLES LIKE 'lower_case_table_names' >>> >>>> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine () >>> >>>> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine SHOW >>> >>>> >> COLLATION >>> >>>> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine () >>> >>>> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine SHOW >>> >>>> >> VARIABLES LIKE 'sql_mode' >>> >>>> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine () >>> >>>> >> 2011-12-04 16:54:28,618 INFO sqlalchemy.engine.base.Engine BEGIN >>> >>>> >> (implicit) >>> >>>> >> 2011-12-04 16:54:28,620 INFO sqlalchemy.engine.base.Engine SELECT >>> >>>> >> user.id AS user_id, user.email AS user_email, user.password AS >>> >>>> >> user_password, user.viewpassword AS user_viewpassword >>> >>>> >> FROM user >>> >>>> >> WHERE user.email = %s >>> >>>> >> LIMIT %s, %s >>> >>>> >> 2011-12-04 16:54:28,621 INFO sqlalchemy.engine.base.Engine >>> >>>> >> ('tom@google.com', 0, 1) >>> >>>> >> >>> user >>> >>>> >> >>> >>>> >> >>> >>>> >> >>> >>>> >> <User('u'tom@google.com'','u'sha1$rshps9X0$ff4c48176df5c86f0042db0746479fbebacbbc66'')> >>> >>>> >> >>> user.viewpassword = 'hi' >>> >>>> >> >>> db.session.commit() >>> >>>> >> >>> >>>> >> As far as I know, this should work, but when I check my database, >>> >>>> >> user.viewpassword is still null. >>> >>>> >> Any ideas? >>> >>>> > >>> >>>> > >>> >>> >>> >>> >>> >> >> >> >
You are right, Adam! My mistake. On Mon, Dec 5, 2011 at 4:30 PM, Adam Patterson <fakeempire@gmail.com> wrote: > This is a quote from Armin: > "Unnecessary for modifications. The object is automatically part of > the session if it was retrieved via Object.query." > > Here is the context: > > http://flask.pocoo.org/mailinglist/archive/2011/10/18/updating-an-existing-record-with-sqlalchemy/ > > You can see Armin's response about 5 messages down. > > > On Mon, Dec 5, 2011 at 6:16 PM, Andrey V. Martyanov <realduke@gmail.com> > wrote: > > Adam, added by whom? You should explicitly add an object to the session. > > > > > >> On Mon, Dec 5, 2011 at 11:45 AM, Adam Patterson <fakeempire@gmail.com> > >> wrote: > >>> > >>> Correct me if I'm wrong but I don't think you need to add user to the > >>> session when you are modifying existing records. I believe its already > >>> added. > >>> > >>> On Mon, Dec 5, 2011 at 11:05 AM, Thomas Dziedzic <gostrc@gmail.com> > >>> wrote: > >>> > Ah, that works, > >>> > but I'm still left a little confused, maybe you can explain this or > >>> > point me to some resources that would explain this; > >>> > > >>> > in my app.py I have something like: > >>> > ... > >>> > db = SQLAlchemy(app) > >>> > import models > >>> > user = User.query.filter_by(email='tom@google.com').first() > >>> > user.viewpassword = 'hi' > >>> > db.session.add(user) > >>> > > >>> > and this doesn't work, > >>> > > >>> > but if I import db from models, it seems to work, > >>> > so it looks like db is different in app.py and models.py > >>> > I'm confused as how this works and how I could use the right db in > >>> > app.py so that something like db.session.add(user) would work in > >>> > app.py and not just an external file. > >>> > > >>> > basically I want to have the controller portion in app.py and the > >>> > models seperated into models.py. Is there a clean way of doing this? > >>> > > >>> > Thanks for your time and answers. > >>> > > >>> > On Sun, Dec 4, 2011 at 7:59 PM, Andrey V. Martyanov > >>> > <realduke@gmail.com> wrote: > >>> >> For your specific situation the following should work: > >>> >> > >>> >> app.py > >>> >> > >>> >> from flask import Flask > >>> >> app = Flask(__name__) > >>> >> app.config['SQLALCHEMY_DATABASE_URI'] = > >>> >> 'mysql://root:tom123@localhost/idabble' > >>> >> from flaskext.sqlalchemy import SQLAlchemy > >>> >> db = SQLAlchemy(app) > >>> >> > >>> >> model.py > >>> >> > >>> >> from app import db > >>> >> > >>> >> class User(db.Model): > >>> >> # Provide your model definition > >>> >> pass > >>> >> > >>> >>>>> from model import db, User > >>> >>>>> user = User.query.filter_by(email='tom@google.com').first() > >>> >>>>> user.viewpassword = 'hi' > >>> >>>>> db.session.add(user) > >>> >>>>> db.session.commit() > >>> >> > >>> >> On Mon, Dec 5, 2011 at 6:44 AM, Andrey V. Martyanov > >>> >> <realduke@gmail.com> > >>> >> wrote: > >>> >>> > >>> >>> It's unclear to me what your model contains. If you want to > separate > >>> >>> the > >>> >>> main application initialization from models then you need to > >>> >>> explicitly > >>> >>> import SQLAlchemy instance and use it's Model class as a base class > >>> >>> for all > >>> >>> your models. > >>> >>> > >>> >>> Example, based on your code: > >>> >>> > >>> >>> app.py > >>> >>> > >>> >>> from flask import Flask > >>> >>> app = Flask(__name__) > >>> >>> app.config['SQLALCHEMY_DATABASE_URI'] = > >>> >>> 'mysql://root:tom123@localhost/idabble' > >>> >>> from flaskext.sqlalchemy import SQLAlchemy > >>> >>> db = SQLAlchemy(app) > >>> >>> > >>> >>> model.py > >>> >>> > >>> >>> from .app import db > >>> >>> > >>> >>> class User(db.Model): > >>> >>> pass > >>> >>> > >>> >>> On Mon, Dec 5, 2011 at 4:55 AM, Thomas Dziedzic <gostrc@gmail.com> > >>> >>> wrote: > >>> >>>> > >>> >>>> Hi, thanks for the suggestion, but it didn't work: > >>> >>>> > >>> >>>> I put this right before db.session.commit() and got the following > >>> >>>> output > >>> >>>> > >>> >>>> >>> db.session.add(user) > >>> >>>> Traceback (most recent call last): > >>> >>>> File "<input>", line 1, in <module> > >>> >>>> File > "/usr/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", > >>> >>>> line 113, in do > >>> >>>> return getattr(self.registry(), name)(*args, **kwargs) > >>> >>>> File > "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", > >>> >>>> line 1161, in add > >>> >>>> self._save_or_update_state(state) > >>> >>>> File > "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", > >>> >>>> line 1170, in _save_or_update_state > >>> >>>> self._save_or_update_impl(state) > >>> >>>> File > "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", > >>> >>>> line 1402, in _save_or_update_impl > >>> >>>> self._update_impl(state) > >>> >>>> File > "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", > >>> >>>> line 1394, in _update_impl > >>> >>>> self._attach(state) > >>> >>>> File > "/usr/lib/python2.7/site-packages/sqlalchemy/orm/session.py", > >>> >>>> line 1428, in _attach > >>> >>>> state.session_id, self.hash_key)) > >>> >>>> InvalidRequestError: Object '<User at 0x1a75910>' is already > >>> >>>> attached > >>> >>>> to session '15348048' (this is '15347920') > >>> >>>> > >>> >>>> > >>> >>>> On Sun, Dec 4, 2011 at 5:32 PM, Andrey V. Martyanov > >>> >>>> <realduke@gmail.com> > >>> >>>> wrote: > >>> >>>> > Hi! > >>> >>>> > > >>> >>>> > You should add your user object to the session: > >>> >>>> > > >>> >>>> >>> db.session.add(user) > >>> >>>> > > >>> >>>> > then commit: > >>> >>>> > > >>> >>>> >>> db.session.commit() > >>> >>>> > > >>> >>>> > > >>> >>>> > On Mon, Dec 5, 2011 at 4:09 AM, Thomas Dziedzic < > gostrc@gmail.com> > >>> >>>> > wrote: > >>> >>>> >> > >>> >>>> >> I already have a user in a database with a field named > >>> >>>> >> user.viewpassword which isn't displayed in __repr__. > >>> >>>> >> The following is the series of commands I do: > >>> >>>> >> > >>> >>>> >> >>> from flask import Flask > >>> >>>> >> >>> app = Flask(__name__) > >>> >>>> >> >>> app.config['SQLALCHEMY_DATABASE_URI'] = > >>> >>>> >> >>> 'mysql://root:tom123@localhost/idabble' > >>> >>>> >> >>> from flaskext.sqlalchemy import SQLAlchemy > >>> >>>> >> >>> db = SQLAlchemy(app) > >>> >>>> >> >>> import models > >>> >>>> >> >>> user = > >>> >>>> >> >>> models.User.query.filter_by(email='tom@google.com > ').first() > >>> >>>> >> 2011-12-04 16:54:28,599 INFO sqlalchemy.engine.base.Engine > SELECT > >>> >>>> >> DATABASE() > >>> >>>> >> 2011-12-04 16:54:28,600 INFO sqlalchemy.engine.base.Engine () > >>> >>>> >> 2011-12-04 16:54:28,603 INFO sqlalchemy.engine.base.Engine SHOW > >>> >>>> >> VARIABLES LIKE 'character_set%%' > >>> >>>> >> 2011-12-04 16:54:28,604 INFO sqlalchemy.engine.base.Engine () > >>> >>>> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine SHOW > >>> >>>> >> VARIABLES LIKE 'lower_case_table_names' > >>> >>>> >> 2011-12-04 16:54:28,605 INFO sqlalchemy.engine.base.Engine () > >>> >>>> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine SHOW > >>> >>>> >> COLLATION > >>> >>>> >> 2011-12-04 16:54:28,607 INFO sqlalchemy.engine.base.Engine () > >>> >>>> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine SHOW > >>> >>>> >> VARIABLES LIKE 'sql_mode' > >>> >>>> >> 2011-12-04 16:54:28,617 INFO sqlalchemy.engine.base.Engine () > >>> >>>> >> 2011-12-04 16:54:28,618 INFO sqlalchemy.engine.base.Engine > BEGIN > >>> >>>> >> (implicit) > >>> >>>> >> 2011-12-04 16:54:28,620 INFO sqlalchemy.engine.base.Engine > SELECT > >>> >>>> >> user.id AS user_id, user.email AS user_email, user.password AS > >>> >>>> >> user_password, user.viewpassword AS user_viewpassword > >>> >>>> >> FROM user > >>> >>>> >> WHERE user.email = %s > >>> >>>> >> LIMIT %s, %s > >>> >>>> >> 2011-12-04 16:54:28,621 INFO sqlalchemy.engine.base.Engine > >>> >>>> >> ('tom@google.com', 0, 1) > >>> >>>> >> >>> user > >>> >>>> >> > >>> >>>> >> > >>> >>>> >> > >>> >>>> >> <User('u'tom@google.com > '','u'sha1$rshps9X0$ff4c48176df5c86f0042db0746479fbebacbbc66'')> > >>> >>>> >> >>> user.viewpassword = 'hi' > >>> >>>> >> >>> db.session.commit() > >>> >>>> >> > >>> >>>> >> As far as I know, this should work, but when I check my > database, > >>> >>>> >> user.viewpassword is still null. > >>> >>>> >> Any ideas? > >>> >>>> > > >>> >>>> > > >>> >>> > >>> >>> > >>> >> > >> > >> > > >