[sqlalchemy] Handling unique key violations with bulk inserts of large number of rows

2015-01-22 Thread alchemy1
If I'm doing a bulk insert of a very large number of rows is it possible to add only the ones that don't violate unique constraints and log the rest? Since I'm inserting so many rows I don't do a flush/commit after each one, instead I let my application batch several hundred at a time then do a

[sqlalchemy] Can't make custom Enum type

2015-01-13 Thread alchemy1
I'm trying to make a custom Enum type, but can't seem to do so. This sample code shows what I'm trying to do: class Choice(sqlalchemy.types.TypeDecorator): impl = sqlalchemy.dialects.postgresql.ENUM def __init__(self): # super(Choice, self).__init__('a', 'b', 'c', name='d')

[sqlalchemy] Efficient dev workflow for keeping test database in sync with dev database?

2015-01-08 Thread alchemy1
On Postgres, I run tests against a database which require that the tables already exist. The table definitions in the test db need to be identical to the table definitions in the dev db (in the test db they should be empty though). My development workflow is that I change model classes in my

[sqlalchemy] How to make defaults appear in the instance after Session flush?

2015-01-05 Thread alchemy1
If I have a parent object with a one to many relationship to children objects, then when I flush the session I don't see any default values when I inspect the children. In this example, for the first object I can see attribute '_sa_instance_state', 'first', and 'parent_pk'. For the second

[sqlalchemy] ResourceClosedError on Postgres

2014-12-20 Thread alchemy1
I'm getting this error with the following code. Unfortunately none of the code in the stacktrace is my code, it's all SQLAlchemy stuff, so I can't tell where I'm causing the problem. Traceback (most recent call last): File

[sqlalchemy] Re: ResourceClosedError on Postgres

2014-12-20 Thread alchemy1
Also my DBSession is defined like this DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension ())) And I've enabled pyramid_tm in my development.ini file for Pyramid. On Sunday, December 21, 2014 1:09:58 AM UTC+1, alchemy1 wrote: I'm getting this error with the following

[sqlalchemy] Unclear on a few things about the transactional rollback-style of unit testing

2014-12-16 Thread alchemy1
I'm getting this warning from my test code, so I know I'm setting something up wrong. If I have 10 test methods, I get this warning always on the second test method. Never any of the other ones. If I switch the order of test methods around I still get an error on the second method. SAWarning:

[sqlalchemy] Re: Foreign key IntegrityError, do I need to flush the first object before adding the second?

2014-12-09 Thread alchemy1
) is not present in table parent. 'INSERT INTO child (.. On Saturday, December 6, 2014 9:55:10 PM UTC+1, alchemy1 wrote: I have an object that has a foreign key to another, declared like @declared_attr def object_uuid(cls): return Column(UUID, ForeignKey('object.uuid

[sqlalchemy] Foreign key IntegrityError, do I need to flush the first object before adding the second?

2014-12-06 Thread alchemy1
I have an object that has a foreign key to another, declared like @declared_attr def object_uuid(cls): return Column(UUID, ForeignKey('object.uuid'), nullable=False) I then do something like DBSession.add(object) DBSession.add(dependent_object) Where DBSession is declared as

[sqlalchemy] Update an object without first selecting it in ORM

2014-09-08 Thread alchemy1
When using ORM, is there a way to update without first selecting an object? For example, if I am updating a User record's name in the database and I have its primary key, I'd like to just execute one query and update it straight away. But in order to do so I must run 2 queries, first to select

[sqlalchemy] Plain SQLAlchemy vs ORM

2014-09-08 Thread alchemy1
In one of the YouTube videos on SQLAlchemy Mike said that some of the large users of SQLAlchemy prefer to use plain SQLAlchemy instead of SQLAlchemy ORM. What are the reasons behind that? I'm wondering because I started off with ORM and though I like it I've been wondering whether I might be

[sqlalchemy] Handling orphans when overwriting one-to-many list with brand new list

2014-08-26 Thread alchemy1
I have these classes, with a one-to-many relationship from User to Thing. Thing has a composite primary key of the user_uuid and the thing_type. class User(Base): ... uuid = Column(UUID, primary_key=True) things = relationship('Thing') ... class Thing(Base): ...

[sqlalchemy] Adding timezone to TIMESTAMP (Postgres) in Alembic

2014-08-26 Thread alchemy1
I added a Postgres TIMESTAMP column and ran a migration, then realized I wanted to do TIMESTAMP(timezone=True) so I updated my models then ran another migration, but the migration files had a 'pass' in the upgrade/downgrade functions rather than doing anything. I had to revert to the migration

[sqlalchemy] Committing/flushing DBSession explicitly

2014-08-23 Thread alchemy1
I've been following the pattern of (in Pyramid webapps) doing DBSession.add then letting the web framework flush/commit the DBSession at the end of the web request rather than doing it explicitly. Are there cases where doing it this way could cause unexpected results, for example let's say you

Re: [sqlalchemy] Trying to get tables to be created by unit tests

2014-08-12 Thread alchemy1
/11/2014 04:37 PM, alchemy1 wrote: I have combined several examples I've found to try to get the 'transactional-style' of unit tests to work, where you roll back the database after each test. However when I run this, the test fails when trying to insert the object with DBSession.add

Re: [sqlalchemy] Trying to get tables to be created by unit tests

2014-08-12 Thread alchemy1
': one} I'm getting an error: sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper|MyModel|models, SQL expression or this Session Any ideas? On Tuesday, August 12, 2014 11:44:05 AM UTC+2, alchemy1 wrote: DBSession is like this (not even sure if I need to do

[sqlalchemy] Trying to get tables to be created by unit tests

2014-08-11 Thread alchemy1
I have combined several examples I've found to try to get the 'transactional-style' of unit tests to work, where you roll back the database after each test. However when I run this, the test fails when trying to insert the object with DBSession.add, complaining that the tables don't exist. I

[sqlalchemy] Use a string or reference in ForeignKey?

2014-08-10 Thread alchemy1
I see that in ForeignKey I can use either a string or reference, so ForeignKey(MyModel.id) or ForeignKey('my_model.id'). Any advantage to using one or the other? What's the recommended approach? When using the string way, is it recommended to use the model name like 'MyModel.id' or table name