[sqlalchemy] Re: How to define a column as read-only (e.g. computed by column in Firebird)
Mike, Michael Bayer wrote: ... oh sorry, I misread the source code in SA earlier...for an INSERT, we are going to insert None for all columns that are blank but dont have a default. so your two options here are to put another PassiveDefault on the column: Column(u'consumedvalue', Numeric(precision=18,length=2,asdecimal=True), PassiveDefault()), That did the trick. Thanks a lot Werner --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: How to define a column as read-only (e.g. computed by column in Firebird)
Mike, Michael Bayer wrote: On Nov 7, 2007, at 12:32 PM, Werner F. Bruhin wrote: Can you point out what I did wrong, please. id have to see a larger example, seems like something is marking the column as modified when it should not be. I put a little test case together, which does very little but I get the exception. engine = sa.create_engine(url, encoding='utf8', echo=True) Session = sao.sessionmaker(autoflush=True, transactional=True) Session.configure(bind=engine) session = Session() dbItemConsumption = db.Consumption() dbItemConsumption.fk_cbbottleid = 33 session.save(dbItemConsumption) session.flush session.commit() Here the echo output and the exception is: 2007-11-07 18:52:53,438 INFO sqlalchemy.engine.base.Engine.0x..d0 BEGIN 2007-11-07 18:52:53,460 INFO sqlalchemy.engine.base.Engine.0x..d0 SELECT gen_id(gen_consumption_consumptionid, 1) FROM rdb$database 2007-11-07 18:52:53,460 INFO sqlalchemy.engine.base.Engine.0x..d0 None 2007-11-07 18:52:53,463 INFO sqlalchemy.engine.base.Engine.0x..d0 INSERT INTO consumption (consumptionid, quantity, unitprice, consumedvalue, remarks, consumed, maturityfirst, maturitybest, maturitypast, created, updated, fk_reasonid, fk_cbbottleid, transferid, fk_cellarid, fk_ratingid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 2007-11-07 18:52:53,464 INFO sqlalchemy.engine.base.Engine.0x..d0 [58, None, None, None, None, None, None, None, None, None, None, None, 33, None, None, None] 2007-11-07 18:52:53,466 INFO sqlalchemy.engine.base.Engine.0x..d0 ROLLBACK Traceback (most recent call last): File saTest.py, line 27, in module session.commit() File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\session.py, line 483, in commit self.transaction = self.transaction.commit() File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\session.py, line 210, in commit self.session.flush() File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\session.py, line 681, in flush self.uow.flush(self, objects) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 216, in flush flush_context.execute() File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 432, in execute UOWExecutor().execute(self, head) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1051, in execute self.execute_save_steps(trans, task) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1070, in execute_save_steps self.execute_childtasks(trans, task, False) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1088, in execute_childtasks self.execute(trans, child, isdelete) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1051, in execute self.execute_save_steps(trans, task) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1070, in execute_save_steps self.execute_childtasks(trans, task, False) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1088, in execute_childtasks self.execute(trans, child, isdelete) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1051, in execute self.execute_save_steps(trans, task) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1070, in execute_save_steps self.execute_childtasks(trans, task, False) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1088, in execute_childtasks self.execute(trans, child, isdelete) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1051, in execute self.execute_save_steps(trans, task) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1070, in execute_save_steps self.execute_childtasks(trans, task, False) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1088, in execute_childtasks self.execute(trans, child, isdelete) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1051, in execute self.execute_save_steps(trans, task) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1070, in execute_save_steps self.execute_childtasks(trans, task, False) File c:\python25\lib\site-packages\SQLAlchemy-0.4.0-py2.5.egg\sqlalchemy\orm\unitofwork.py, line 1088, in execute_childtasks self.execute(trans, child, isdelete) File
[sqlalchemy] Re: How to define a column as read-only (e.g. computed by column in Firebird)
On Nov 7, 2007, at 10:23 AM, Werner F. Bruhin wrote: How can I define a column in the table as read-only? I have some computed by columns, e.g.: consumedvalue computed by (quantity*unitprice) These columns can not be updated, otherwise I get the following exception: ProgrammingError: (ProgrammingError) (-151, 'isc_dsql_prepare: \n attempted update of read-only column') read-only behavior is a class-level thing, so something like this: class MyClass(object): def mycol(self): return self._mycol mycol = property(mycol) mapper(MyClass, mytable, properties={ '_mycol':mytable.c.mycol, 'mycol':synonym('_mycol') }) Theres a ticket in trac which will make the above configuration slightly less verbose in a future release. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: How to define a column as read-only (e.g. computed by column in Firebird)
On Nov 7, 2007, at 12:58 PM, Werner F. Bruhin wrote: Mike, Michael Bayer wrote: On Nov 7, 2007, at 12:32 PM, Werner F. Bruhin wrote: Can you point out what I did wrong, please. id have to see a larger example, seems like something is marking the column as modified when it should not be. I put a little test case together, which does very little but I get the exception. oh sorry, I misread the source code in SA earlier...for an INSERT, we are going to insert None for all columns that are blank but dont have a default. so your two options here are to put another PassiveDefault on the column: Column(u'consumedvalue', Numeric(precision=18,length=2,asdecimal=True), PassiveDefault()), or you can leave the column out of the mapping entirely (but then reading it requires an extra query): mapper(Consumption, consumption_table, exclude_properties=['consumedvalue']) the consumedvalue column will post-fetch after an INSERT statement. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: How to define a column as read-only (e.g. computed by column in Firebird)
On Nov 7, 2007, at 12:32 PM, Werner F. Bruhin wrote: Can you point out what I did wrong, please. id have to see a larger example, seems like something is marking the column as modified when it should not be. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: How to define a column as read-only (e.g. computed by column in Firebird)
Mike, Michael Bayer wrote: On Nov 7, 2007, at 10:23 AM, Werner F. Bruhin wrote: How can I define a column in the table as read-only? I have some computed by columns, e.g.: consumedvalue computed by (quantity*unitprice) These columns can not be updated, otherwise I get the following exception: ProgrammingError: (ProgrammingError) (-151, 'isc_dsql_prepare: \n attempted update of read-only column') read-only behavior is a class-level thing, so something like this: class MyClass(object): def mycol(self): return self._mycol mycol = property(mycol) mapper(MyClass, mytable, properties={ '_mycol':mytable.c.mycol, 'mycol':synonym('_mycol') }) Theres a ticket in trac which will make the above configuration slightly less verbose in a future release. I don't have many of these, so no big deal that it is verbose. However I must not do something wrong in transposing the above as I still get the error. I must be a bit dense on this, here is what I have done: consumption_table = sa.Table(u'consumption', metadata, sa.Column(u'consumptionid', sa.Integer(), sa.Sequence('gen_consumption_consumptionid'), primary_key=True, nullable=False), sa.Column(u'quantity', sa.Integer()), sa.Column(u'unitprice', sa.Numeric(precision=18,length=2,asdecimal=True)), sa.Column(u'consumedvalue', sa.Numeric(precision=18,length=2,asdecimal=True)), ) class Consumption(object): def consumedvalue(self): return self._consumedvalue consumedvalue = property(consumedvalue) pass consumption = sao.mapper(Consumption, consumption_table, properties={ ... 'cellar': sao.relation(Cellar, backref='consumption'), '_consumedvalue': consumption_table.c.consumedvalue, 'consumedvalue': sao.synonym('_consumedvalue'), }) Can you point out what I did wrong, please. Werner --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---