[sqlalchemy] CheckConstraints defined as an expression on a ColumnClause

2017-04-03 Thread Luca Wehrstedt
Hi, I was trying to define a subclass of CheckConstraint to match a column (whose name is given) to a regexp. class CodenameConstraint(CheckConstraint): """Check that the column uses a limited alphabet.""" def __init__(self, column_name): column = ColumnClause(column_name)

Re: [sqlalchemy] Large number of polymorphic subclasses

2017-04-03 Thread damon
Hey Mike, Looks like I spoke too soon -- a few more questions: Using the example code you posted, we're actually seeing 4 additional queries (one per result model), rather than the expected 3 (one per result model type). If you print context.query inside load_extra, I think it's clear why: -

Re: [sqlalchemy] Large number of polymorphic subclasses

2017-04-03 Thread damon
Thanks a ton for your help, Mike! We played around with it and are pretty happy with your solution using the load() event, so we'll be using that moving forward. Damon On Wednesday, March 29, 2017 at 2:40:39 PM UTC-7, Mike Bayer wrote: > > I have a working version of both loading relationships

Re: [sqlalchemy] Re: Help with ORM join syntax

2017-04-03 Thread Jonathan Vanasco
On Monday, April 3, 2017 at 7:09:41 PM UTC-4, Jason T. wrote: > > I think there may be even a cleaner way to do this since I have > relationships() built into the models, > In that case, you will need to use "contains_eager" if you play on iterating on any of the relationships http://d

Re: [sqlalchemy] Re: Help with ORM join syntax

2017-04-03 Thread Jason T.
Yes, I had read the documentation. I am just new to SQL and databases in general. I think there may be even a cleaner way to do this since I have relationships() built into the models, but since the tables have multiple foreign keys I think I had to specify the "on clause" anyhow. Thanks for th

Re: [sqlalchemy] Re: Help with ORM join syntax

2017-04-03 Thread mike bayer
Have you read the tutorial at http://docs.sqlalchemy.org/en/rel_1_1/orm/tutorial.html#querying-with-joins and read all the examples at http://docs.sqlalchemy.org/en/rel_1_1/orm/query.html#sqlalchemy.orm.query.Query.join? There's nothing odd about the joins you're trying to build here. Yo

Re: [sqlalchemy] Re: Joining an aliased model to a relationship with a primaryjoin

2017-04-03 Thread mike bayer
On 04/03/2017 04:31 PM, Colton Allen wrote: No worries, we ended up dropping the relationship. Still a curious error, though. not too much, the relationship was definitely leaking between what should normally be split across primaryjoin/secondaryjoin if the "secondary" argument is in place

Re: [sqlalchemy] Issue with postgres insert on_conflict when using a uniqueConstraint

2017-04-03 Thread mike bayer
noting that your example is erroneously referring to "mysql", the trace you describe is against Postgresql, issue https://bitbucket.org/zzzeek/sqlalchemy/issues/3955/connectionless-execution-autocommit-on is added and fix targeted at 1.1.9 is at https://gerrit.sqlalchemy.org/#/q/I235a25daf4381b

[sqlalchemy] Re: Help with ORM join syntax

2017-04-03 Thread Jason T.
Okay. I figured out how to use the ORM without joins, but I still can't figure out how to use the joins. :( bus1_alias = aliased(sam.Bus) bus2_alias = aliased(sam.Bus) branch_db = self.db_hook.session.query(sam.Branch). \ filter(sam.Branch.id_model == sam.Bus.id_model). \ filter(sam.Bran

[sqlalchemy] Re: Joining an aliased model to a relationship with a primaryjoin

2017-04-03 Thread Colton Allen
No worries, we ended up dropping the relationship. Still a curious error, though. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflo

Re: [sqlalchemy] Joining an aliased model to a relationship with a primaryjoin

2017-04-03 Thread mike bayer
On 04/03/2017 03:19 PM, Colton Allen wrote: | class EntryModel(Model): word_count = db.relationship( 'WordCountModel', backref='entry', secondary='transcription', primaryjoin='and_(' 'WordCountModel.transcription_id == TranscriptionModel.id,' 'TranscriptionModel

Re: [sqlalchemy] automap with self-referential relationship

2017-04-03 Thread yoch . melka
Thank tou, I solved the problem with this code : def name_for_collection_relationship(base, local_cls, referred_cls, constraint): disc = '_'.join(col.name for col in constraint.columns) return referred_cls.__name__.lower() + '_' + disc + "_collection" Le lundi 3 avril 2017 17:41:20 UTC

[sqlalchemy] Joining an aliased model to a relationship with a primaryjoin

2017-04-03 Thread Colton Allen
class EntryModel(Model): word_count = db.relationship( 'WordCountModel', backref='entry', secondary='transcription', primaryjoin='and_(' 'WordCountModel.transcription_id == TranscriptionModel.id,' 'TranscriptionModel.entry_id == EntryModel.id,' 'Transcriptio

[sqlalchemy] Help with ORM join syntax

2017-04-03 Thread Jason T.
All, I am able to join two tables and get the desired result with the below SQL syntax; however, I am having trouble translating this SQL to the ORM join syntax. Any help will be appreciated. SELECT * FROM raw.branch as b JOIN raw.bus AS bus1 ON bus1.id = b.id_from_bus AND bus1.id_model

[sqlalchemy] Issue with postgres insert on_conflict when using a uniqueConstraint

2017-04-03 Thread nicolas . rolin
Hello everyone, I encountered a surprising behavior of the postres specific insert when using UniqueConstraint. A test code : from sqlalchemy import Column, Integer, String, UniqueConstraint, create_engine from sqlalchemy.dialects.postgresql import insert as pg_insert from sqlalchemy.ext.decla

Re: [sqlalchemy] Delete orphan occurs even before commit happens and when expunge happens

2017-04-03 Thread mike bayer
On 04/03/2017 11:48 AM, Shane Carey wrote: So the reason the children are marked for delete is because they are no longer associated with the parent, and for that same reason the expunge does not propagate to them. As much as I wish this would work for me, I have to agree with you. Is there an

Re: [sqlalchemy] Delete orphan occurs even before commit happens and when expunge happens

2017-04-03 Thread Shane Carey
So the reason the children are marked for delete is because they are no longer associated with the parent, and for that same reason the expunge does not propagate to them. As much as I wish this would work for me, I have to agree with you. Is there any way to prevent the delete from occurring on

Re: [sqlalchemy] Session execution_options are lost after commit

2017-04-03 Thread mike bayer
On 04/03/2017 09:53 AM, Артём Мигда wrote: Hi, I trying to use schema_translate_map in my project and I encountered with strange behavior of execution_options binding. Here's a code with session creation: DBSession = scoped_session(sessionmaker()) session = DBSession() session.connection(exec

Re: [sqlalchemy] Delete orphan occurs even before commit happens and when expunge happens

2017-04-03 Thread mike bayer
On 04/03/2017 09:52 AM, Shane Carey wrote: I am getting an error where delete orphan on a relationship seems to happen even when the parent object is expunged. I reproduced the issue in this example. from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import

Re: [sqlalchemy] automap with self-referential relationship

2017-04-03 Thread mike bayer
hi - this is perhaps a case automap should come up with something easier for, and at least have a note in the docs that refers to this specifically, however the general approach is to apply a naming convention to the relationships that will allow the conflict to resolve. you'd define name_f

[sqlalchemy] Session execution_options are lost after commit

2017-04-03 Thread Артём Мигда
Hi, I trying to use schema_translate_map in my project and I encountered with strange behavior of execution_options binding. Here's a code with session creation: DBSession = scoped_session(sessionmaker()) session = DBSession() session.connection(execution_options={ 'schema_translate_map`: {

[sqlalchemy] Delete orphan occurs even before commit happens and when expunge happens

2017-04-03 Thread Shane Carey
I am getting an error where delete orphan on a relationship seems to happen even when the parent object is expunged. I reproduced the issue in this example. from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import * Base = declarative_base() class Parent(Bas