Re: [sqlalchemy] How to ignore primary key errors on insert

2018-08-21 Thread Jonathan Vanasco
There's also the strategy of doing something within a nested transaction, 
which will allow you to rollback on an integrity error.

such as...


try:
with s.begin_nested():
# do stuff
s.flush()  # this will trigger an integrity error, unless the fkey 
checks are deferred
except exceptions.IntegrityError:
pass

-- 
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://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Remote side backpopulates

2018-08-21 Thread Mike Bayer
On Tue, Aug 21, 2018 at 12:47 PM, Alex Rothberg  wrote:
> Is there any way to declare a "remote side" backpopulates? i.e. where I
> declare a relationship on class A to appear only on class B? I would like
> the relationship only to be available on the remote class but I do not want
> to / cannot modify the code for the remote class.


this is Python, so "code" is just another word for "objects
dynamically created and linked together".  Just add the relationship
as you want:

User.permissions = relationship(Permission)



>
> For example:
>
> class User(Model):
> ...
>
> class Permission(Model):
> user_active = relationship(User, backpopulates_remote='permissions')
>
>
> where the user_active field does not get created on Permission but the
> permissions field does get created on User? This comes up since I end up
> writing:
>
> class Permission(Model):
> user = relationship(User)
> user_active = relationship(User, backref='permissions',
> primary_join=...is_active...)
>
> and I do want the user field on Permission and the permissions field on
> User, but I do not want the user_active, as it is extraneous.
>
> --
> 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://stackoverflow.com/help/mcve for a full
> description.
> ---
> 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 https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
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://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Remote side backpopulates

2018-08-21 Thread Alex Rothberg
Is there any way to declare a "remote side" backpopulates? i.e. where I 
declare a relationship on class A to appear only on class B? I would like 
the relationship only to be available on the remote class but I do not want 
to / cannot modify the code for the remote class.

For example:

class User(Model):
...

class Permission(Model):
user_active = relationship(User, backpopulates_remote='permissions')


where the user_active field does not get created on Permission but the 
permissions field does get created on User? This comes up since I end up 
writing:

class Permission(Model):
user = relationship(User)
user_active = relationship(User, backref='permissions', primary_join=...
is_active...)

and I do want the user field on Permission and the permissions field on 
User, but I do not want the user_active, as it is extraneous.

-- 
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://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] How to ignore primary key errors on insert

2018-08-21 Thread Simon King
If you are using SQLAlchemy core, there's this:

http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#insert-on-conflict-upsert

Does that meet your needs?

Simon

On Tue, Aug 21, 2018 at 3:36 AM Hardik Sanghavi  wrote:
>
> Did this get resolved or are we to still ignore it
>
>
> On Wednesday, May 7, 2008 at 12:40:23 AM UTC+10, Michael Bayer wrote:
>>
>>
>> just use try/except
>>
>> from sqlalchemy import exceptions
>>
>> try:
>>  engine.execute(mytable.insert(), **kwargs)
>> except exceptions.IntegrityError, e:
>> print "Error !", e, "well, I guess we'll ignore it."
>>
>> engine.execute(some_other_insert_statement ... )
>>
>> On May 6, 2008, at 10:06 AM, Alexis B wrote:
>>
>> >
>> > Hi to all,
>> >
>> > This may be a newbie question but I just can't find the answer. I have
>> > to make multiple submissions to a postgresql table and I want to use
>> > python. I found everything to execute my insert commands, the problem
>> > is that I have to repeat it regularly, and I expected not to check
>> > which record have already inserted thanks to the primary key ( which
>> > is a couple if integer I set ).
>> > So when it tries to insert again some records, it doesn't insert it,
>> > as expected, but it raises an error which interrupt the script :
>> >
>> > **
>> > Traceback (most recent call last):
>> >  File "./script.py", line 44, in 
>> >connection.execute(line)
>> >  File " . . ./python-2.5.1/lib/python2.5/site-packages/
>> > SQLAlchemy-0.4.5-py2.5.egg/sqlalchemy/engine/base.py", line 844, in
>> > execute
>> >return Connection.executors[c](self, object, multiparams, params)
>> >  File ". . ./python-2.5.1/lib/python2.5/site-packages/
>> > SQLAlchemy-0.4.5-py2.5.egg/sqlalchemy/engine/base.py", line 854, in
>> > _execute_text
>> >self.__execute_raw(context)
>> >  File ". . ./python-2.5.1/lib/python2.5/site-packages/
>> > SQLAlchemy-0.4.5-py2.5.egg/sqlalchemy/engine/base.py", line 916, in
>> > __execute_raw
>> >self._cursor_execute(context.cursor, context.statement,
>> > context.parameters[0], context=context)
>> >  File ". . ./python-2.5.1/lib/python2.5/site-packages/
>> > SQLAlchemy-0.4.5-py2.5.egg/sqlalchemy/engine/base.py", line 960, in
>> > _cursor_execute
>> >self._handle_dbapi_exception(e, statement, parameters, cursor)
>> >  File ". . ./python-2.5.1/lib/python2.5/site-packages/
>> > SQLAlchemy-0.4.5-py2.5.egg/sqlalchemy/engine/base.py", line 942, in
>> > _handle_dbapi_exception
>> >raise exceptions.DBAPIError.instance(statement, parameters, e,
>> > connection_invalidated=is_disconnect)
>> > sqlalchemy.exceptions.IntegrityError: (IntegrityError) duplicate key
>> > violates unique constraint "my_primarykey"
>> > "INSERT INTO . . . )" {}
>> > **
>> >
>> > So I just wanted to know if there was an option to ignore the error ,
>> > which possibly raise it, but don't interrupt the script.
>> >
>> > Thanks
>> >
>> > >
>
> --
> 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://stackoverflow.com/help/mcve for a full 
> description.
> ---
> 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 https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
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://stackoverflow.com/help/mcve for a full description.
--- 
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.