Vitalii Ponomar
Vitalii Ponomar

Reputation: 10946

Pyramid: how to add record to database?

I've tried to add record to database from command line, but it did not work (I mean no record was added to db and no error was raised).

Here is code:

from myproject.models import DBSession, Model

session = DBSession()
md = Model(name='text')
session.add(md)

In models.py DBSession() was defined automatically by scaffold. I've changed only Model structure.

What I did wrong?

Thanks!

Upvotes: 1

Views: 664

Answers (1)

Michael Merickel
Michael Merickel

Reputation: 23331

Likely your session is attached to a transaction manager DBSession = .. extensions=[ZopeTransactionExtension()]), which is not active when using the console. Thus you have to be the transaction manager and do it yourself.

import transaction
transaction.commit()

at the end of your code. Remember that session.add simply adds the object to the session, but doesn't actually flush the commands to the database or commit them.

Upvotes: 7

Related Questions