Re: [sqlalchemy] Problem inserting records in blob column
Hi, I'm experiencing a similar problem in my program (Sqlalchemy 0.8.4). In my case no blob column is involved, the problem seems to be related to presence of a foreign key in the target table. Here is the traceback of the error: File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 721, in commit self.transaction.commit() File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 354, in commit self._prepare_impl() File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 334, in _prepare_impl self.session.flush() File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 1818, in flush self._flush(objects) File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 1936, in _flush transaction.rollback(_capture_exception=True) File C:\Python27\lib\site-packages\sqlalchemy\util\langhelpers.py, line 58, in __exit__ compat.reraise(exc_type, exc_value, exc_tb) File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 1900, in _flush flush_context.execute() File C:\Python27\lib\site-packages\sqlalchemy\orm\unitofwork.py, line 372, in execute rec.execute(self) File C:\Python27\lib\site-packages\sqlalchemy\orm\unitofwork.py, line 525, in execute uow File C:\Python27\lib\site-packages\sqlalchemy\orm\persistence.py, line 64, in save_obj table, insert) File C:\Python27\lib\site-packages\sqlalchemy\orm\persistence.py, line 569, in _emit_insert_statements execute(statement, params) File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 662, in execute params) File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 761, in _execute_clauseelement compiled_sql, distilled_params File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 828, in _execute_context None, None) File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 1024, in _handle_dbapi_exception exc_info File C:\Python27\lib\site-packages\sqlalchemy\util\compat.py, line 196, in raise_from_cause reraise(type(exception), exception, tb=exc_tb) File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 824, in _execute_context context = constructor(dialect, self, conn, *args) File C:\Python27\lib\site-packages\sqlalchemy\engine\default.py, line 446, in _init_compiled self.__process_defaults() File C:\Python27\lib\site-packages\sqlalchemy\engine\default.py, line 821, in __process_defaults val = self.get_insert_default(c) File C:\Python27\lib\site-packages\sqlalchemy\engine\default.py, line 777, in get_insert_default return self._exec_default(column.default, column.type) File C:\Python27\lib\site-packages\sqlalchemy\engine\default.py, line 761, in _exec_default return self.fire_sequence(default, type_) StatementError: 'SQLiteExecutionContext' object has no attribute 'fire_sequence' (original cause: AttributeError: 'SQLiteExecutionContext' object has no attribute 'fire_sequence') u'INSERT INTO dataset (set_id, user_id, label, set_table, nam_table, val_table, datasource, info) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' [{'info': None, 'user_id': 1, 'val_table': u'val_table', 'label': u'tab1', 'nam_tab': u'nam_table', 'set_table': u'tab1', 'datasource': None}] The construct which causes the error is this: dataset.append_constraint(sa.ForeignKeyConstraint([dataset.c.set_id], [task.c.set_id])) If I remove this line of code everything goes well (but of course I cannot set up the relationship between the dataset table and the task table). Any help is really appreciated. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] How to declare a custom type that will imply a check on a column?
The example works wonderfully, however i noticed, that when i attach a constraint to the column, it gets mirrored in the table so the event handler @event.listens_for(PString, after_parent_attach) def create_constraint(target, parent): @event.listens_for(parent, after_parent_attach) def add_to_table(col, table): col.constraints.add(CheckConstraint(parent != '')) will result in CREATE TABLE fun ( name VARCHAR(100) CHECK (name != ''), CHECK (name != '') ) instead of CREATE TABLE fun ( name VARCHAR(100) CHECK (name != '') ) when using m = MetaData() t = Table( 'fun', m, Column('name', PString(100)), ) e = create_engine('sqlite://', echo=1) m.create_all(e) m.drop_all(e) assert 0 On Wednesday, December 18, 2013 5:26:53 PM UTC+1, Michael Bayer wrote: On Dec 18, 2013, at 5:41 AM, RonnyPfannschmidt ronny.pfa...@gmail.comjavascript: wrote: Hi, for supporting a certain schema requirement, i have to create a quite a number of columns that are checked not to be a empty string, So i'd like to declare a custom type, which will automatically imply the check on those columns. I already Investigated, how Boolean does this for databases with non-native boolean types, however it uses number private methods, Should i follow its example or is there a official way? what i currently have to do is Column('foo', String(20), CheckConstraint(foo '') what i'd like use is something like Column('foo', PString(20)) you’d use a TypeDecorator for this. See the example http://docs.sqlalchemy.org/en/rel_0_9/core/types.html#coercing-encoded-strings-to-unicodefor something similar (coerces to utf-8, just replace that with checking for non-empty). -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] How to declare a custom type that will imply a check on a column?
i discovered a workaround by doing @event.listens_for(PString, after_parent_attach) def create_constraint(target, parent): @event.listens_for(parent, after_parent_attach) def add_to_table(col, table): constraint = CheckConstraint(column(col.name) != text(''), _autoattach=False) constraint.parent = col col.constraints.add(constraint) -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Problem inserting records in blob column
there’s a SQLAlchemy issue here, marked as ticket http://www.sqlalchemy.org/trac/ticket/2896, however resolving this issue still won’t fix your issue. You’re emitting an INSERT on a table where the primary key needs to refer to a column elsewhere. that is: task = Table('y', metadata, Column('set_id', Integer, primary_key=True) ) dataset = Table(x, metadata, Column(set_id, Integer, primary_key=True) ) dataset.append_constraint(ForeignKeyConstraint([dataset.c.set_id], [task.c.set_id])) to insert a dataset row, the dataset must refer to a task row. SQLite doesn’t enforce foreign key constraints by default so that the INSERT will proceed but the row will be relationally inconsistent. also, if you aren’t planning on using Oracle as a backend, I’d remove the Sequence directive from your program for now. until #2896 is fixed it will get in the way of allowing this issue to be clear. On Dec 20, 2013, at 5:50 AM, Massi massi_...@msn.com wrote: Hi, I'm experiencing a similar problem in my program (Sqlalchemy 0.8.4). In my case no blob column is involved, the problem seems to be related to presence of a foreign key in the target table. Here is the traceback of the error: File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 721, in commit self.transaction.commit() File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 354, in commit self._prepare_impl() File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 334, in _prepare_impl self.session.flush() File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 1818, in flush self._flush(objects) File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 1936, in _flush transaction.rollback(_capture_exception=True) File C:\Python27\lib\site-packages\sqlalchemy\util\langhelpers.py, line 58, in __exit__ compat.reraise(exc_type, exc_value, exc_tb) File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 1900, in _flush flush_context.execute() File C:\Python27\lib\site-packages\sqlalchemy\orm\unitofwork.py, line 372, in execute rec.execute(self) File C:\Python27\lib\site-packages\sqlalchemy\orm\unitofwork.py, line 525, in execute uow File C:\Python27\lib\site-packages\sqlalchemy\orm\persistence.py, line 64, in save_obj table, insert) File C:\Python27\lib\site-packages\sqlalchemy\orm\persistence.py, line 569, in _emit_insert_statements execute(statement, params) File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 662, in execute params) File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 761, in _execute_clauseelement compiled_sql, distilled_params File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 828, in _execute_context None, None) File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 1024, in _handle_dbapi_exception exc_info File C:\Python27\lib\site-packages\sqlalchemy\util\compat.py, line 196, in raise_from_cause reraise(type(exception), exception, tb=exc_tb) File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 824, in _execute_context context = constructor(dialect, self, conn, *args) File C:\Python27\lib\site-packages\sqlalchemy\engine\default.py, line 446, in _init_compiled self.__process_defaults() File C:\Python27\lib\site-packages\sqlalchemy\engine\default.py, line 821, in __process_defaults val = self.get_insert_default(c) File C:\Python27\lib\site-packages\sqlalchemy\engine\default.py, line 777, in get_insert_default return self._exec_default(column.default, column.type) File C:\Python27\lib\site-packages\sqlalchemy\engine\default.py, line 761, in _exec_default return self.fire_sequence(default, type_) StatementError: 'SQLiteExecutionContext' object has no attribute 'fire_sequence' (original cause: AttributeError: 'SQLiteExecutionContext' object has no attribute 'fire_sequence') u'INSERT INTO dataset (set_id, user_id, label, set_table, nam_table, val_table, datasource, info) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' [{'info': None, 'user_id': 1, 'val_table': u'val_table', 'label': u'tab1', 'nam_tab': u'nam_table', 'set_table': u'tab1', 'datasource': None}] The construct which causes the error is this: dataset.append_constraint(sa.ForeignKeyConstraint([dataset.c.set_id], [task.c.set_id])) If I remove this line of code everything goes well (but of course I cannot set up the relationship between the dataset table and the task table). Any help is really appreciated. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit
Re: [sqlalchemy] Problem inserting records in blob column
issue #2896 is resolved for 0.8.5 and 0.9.0. On Dec 20, 2013, at 10:03 AM, Michael Bayer mike...@zzzcomputing.com wrote: there’s a SQLAlchemy issue here, marked as ticket http://www.sqlalchemy.org/trac/ticket/2896, however resolving this issue still won’t fix your issue. You’re emitting an INSERT on a table where the primary key needs to refer to a column elsewhere. that is: task = Table('y', metadata, Column('set_id', Integer, primary_key=True) ) dataset = Table(x, metadata, Column(set_id, Integer, primary_key=True) ) dataset.append_constraint(ForeignKeyConstraint([dataset.c.set_id], [task.c.set_id])) to insert a dataset row, the dataset must refer to a task row. SQLite doesn’t enforce foreign key constraints by default so that the INSERT will proceed but the row will be relationally inconsistent. also, if you aren’t planning on using Oracle as a backend, I’d remove the Sequence directive from your program for now. until #2896 is fixed it will get in the way of allowing this issue to be clear. On Dec 20, 2013, at 5:50 AM, Massi massi_...@msn.com wrote: Hi, I'm experiencing a similar problem in my program (Sqlalchemy 0.8.4). In my case no blob column is involved, the problem seems to be related to presence of a foreign key in the target table. Here is the traceback of the error: File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 721, in commit self.transaction.commit() File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 354, in commit self._prepare_impl() File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 334, in _prepare_impl self.session.flush() File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 1818, in flush self._flush(objects) File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 1936, in _flush transaction.rollback(_capture_exception=True) File C:\Python27\lib\site-packages\sqlalchemy\util\langhelpers.py, line 58, in __exit__ compat.reraise(exc_type, exc_value, exc_tb) File C:\Python27\lib\site-packages\sqlalchemy\orm\session.py, line 1900, in _flush flush_context.execute() File C:\Python27\lib\site-packages\sqlalchemy\orm\unitofwork.py, line 372, in execute rec.execute(self) File C:\Python27\lib\site-packages\sqlalchemy\orm\unitofwork.py, line 525, in execute uow File C:\Python27\lib\site-packages\sqlalchemy\orm\persistence.py, line 64, in save_obj table, insert) File C:\Python27\lib\site-packages\sqlalchemy\orm\persistence.py, line 569, in _emit_insert_statements execute(statement, params) File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 662, in execute params) File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 761, in _execute_clauseelement compiled_sql, distilled_params File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 828, in _execute_context None, None) File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 1024, in _handle_dbapi_exception exc_info File C:\Python27\lib\site-packages\sqlalchemy\util\compat.py, line 196, in raise_from_cause reraise(type(exception), exception, tb=exc_tb) File C:\Python27\lib\site-packages\sqlalchemy\engine\base.py, line 824, in _execute_context context = constructor(dialect, self, conn, *args) File C:\Python27\lib\site-packages\sqlalchemy\engine\default.py, line 446, in _init_compiled self.__process_defaults() File C:\Python27\lib\site-packages\sqlalchemy\engine\default.py, line 821, in __process_defaults val = self.get_insert_default(c) File C:\Python27\lib\site-packages\sqlalchemy\engine\default.py, line 777, in get_insert_default return self._exec_default(column.default, column.type) File C:\Python27\lib\site-packages\sqlalchemy\engine\default.py, line 761, in _exec_default return self.fire_sequence(default, type_) StatementError: 'SQLiteExecutionContext' object has no attribute 'fire_sequence' (original cause: AttributeError: 'SQLiteExecutionContext' object has no attribute 'fire_sequence') u'INSERT INTO dataset (set_id, user_id, label, set_table, nam_table, val_table, datasource, info) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' [{'info': None, 'user_id': 1, 'val_table': u'val_table', 'label': u'tab1', 'nam_tab': u'nam_table', 'set_table': u'tab1', 'datasource': None}] The construct which causes the error is this: dataset.append_constraint(sa.ForeignKeyConstraint([dataset.c.set_id], [task.c.set_id])) If I remove this line of code everything goes well (but of course I cannot set up the relationship between the dataset table and the task table). Any help is really appreciated. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving
Re: [sqlalchemy] How to declare a custom type that will imply a check on a column?
On Dec 20, 2013, at 8:45 AM, RonnyPfannschmidt ronny.pfannschm...@gmail.com wrote: The example works wonderfully, however i noticed, that when i attach a constraint to the column, it gets mirrored in the table so the event handler @event.listens_for(PString, after_parent_attach) def create_constraint(target, parent): @event.listens_for(parent, after_parent_attach) def add_to_table(col, table): col.constraints.add(CheckConstraint(parent != '’)) use: table.append_constraint(CheckConstraint(parent != '’)) the constraints collection on the Column is handled by the internals. will result in CREATE TABLE fun ( name VARCHAR(100) CHECK (name != ''), CHECK (name != '') ) instead of CREATE TABLE fun ( name VARCHAR(100) CHECK (name != '') ) when using m = MetaData() t = Table( 'fun', m, Column('name', PString(100)), ) e = create_engine('sqlite://', echo=1) m.create_all(e) m.drop_all(e) assert 0 On Wednesday, December 18, 2013 5:26:53 PM UTC+1, Michael Bayer wrote: On Dec 18, 2013, at 5:41 AM, RonnyPfannschmidt ronny.pfa...@gmail.com wrote: Hi, for supporting a certain schema requirement, i have to create a quite a number of columns that are checked not to be a empty string, So i'd like to declare a custom type, which will automatically imply the check on those columns. I already Investigated, how Boolean does this for databases with non-native boolean types, however it uses number private methods, Should i follow its example or is there a official way? what i currently have to do is Column('foo', String(20), CheckConstraint(foo '') what i'd like use is something like Column('foo', PString(20)) you’d use a TypeDecorator for this. See the example http://docs.sqlalchemy.org/en/rel_0_9/core/types.html#coercing-encoded-strings-to-unicode for something similar (coerces to utf-8, just replace that with checking for non-empty). -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email tosqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. signature.asc Description: Message signed with OpenPGP using GPGMail