[sqlalchemy] Re: insertion into association table, giving integrityerror

2017-04-12 Thread shrey . chauhan
Sorry not the first one, but the second one

-- 
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] Re: insertion into association table, giving integrityerror

2017-04-11 Thread shrey . chauhan
Thanks Mike for suggestions, was able to solve both the issues with slight 
changes in the db models

added lazy='dynamic' on both sides where back_populates is being used

-- 
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] Re: insertion into association table, giving integrityerror

2017-04-11 Thread mike bayer



On 04/11/2017 02:37 AM, shrey.chau...@invicto.in wrote:

Yes Mike will do that, mostly its marshmallow model schema which is
creating the object, as I am using Flask + marshmallow for APIs,


p.groups=[] //when i try to empty it, i get this exception
*AssertionError: Collection was loaded during event handling.
 *//though the action is getting performed,
putting a try catch is solving it, but why is it coming?



that error happens means there is an append() or remove() event handler, 
or alternatively a @validates hook for that attribute (which is the same 
thing), which is running probably in the context of a backref being set; 
when the backref is being set, p.groups is not yet loaded.   when the 
handler runs, it is doing something that is causing p.groups to be 
lazy-loaded from the database, rather than just operating upon the 
incoming values.   this is confusing the attribute system with state it 
doesn't expect.   as always, stack trace will tell a lot more here.





though,
g.packages=[]this is working fine with no exception



Thanks for any kind of help, in advance

--
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.


Re: [sqlalchemy] Re: insertion into association table, giving integrityerror

2017-04-11 Thread shrey . chauhan
Yes Mike will do that, mostly its marshmallow model schema which is 
creating the object, as I am using Flask + marshmallow for APIs,

apart from that I have another issue, which i am not able to uderstand:
I have a many-to-many relationship between 2 models, same as posted above, 
still ill put the models here

package model:
class Package(db.Model, BaseMixin):
__tablename__ =  'packages'
__bind_key__  =  'broker_db'
__repr_attrs__=  ["id","name","deletepkg"]
__track_attrs__   =  ["name","versions"]
#attributes
id= db.Column(db.Integer, primary_key=True, 
autoincrement=True)
name  = db.Column(db.String(100),  
index=True,unique=True, nullable=False)
deletepkg = db.Column(db.Boolean,  
index=True,nullable=False)
instances = db.relationship('Instance', 
cascade="all,delete",secondary=PACKAGE_INSTANCE_RELATION,back_populates="packages")
groups= db.relationship('Group',cascade="all,delete", 
secondary=PACKAGE_GROUP_RELATION,back_populates="packages")
versions  = db.relationship('Version',lazy='dynamic')
events= db.relationship('Event', 
backref=db.backref('package', uselist=False),lazy='dynamic')

group model:
PACKAGE_GROUP_RELATION = db.Table('package_group_relation',
db.Column('package_id', db.Integer, db.ForeignKey('packages.id')),
db.Column('groupt_id', db.Integer, db.ForeignKey('groupts.id')),
info={'bind_key': 'broker_db'}
)



class Group(db.Model,BaseMixin):
__tablename__ = 'groupts'
__repr_attrs__= ["id","name"]
__bind_key__  = 'broker_db'
__track_attrs__   = ["name","packages"]
id= db.Column(db.Integer, primary_key=True, 
autoincrement=True)
name  = db.Column(db.String(100),unique=True)
packages  = 
db.relationship("Package",cascade="all,delete",secondary=PACKAGE_GROUP_RELATION,back_populates="groups")
events= db.relationship('Event', 
backref=db.backref('group', uselist=False), lazy='dynamic')
def __init__(self, name):
self.name = name


in this model when i do something like this:
p = Package.query.filter_by(name='firefox').first()   //here p is 
Package object, and it has some groups mapped to it

p.groups=[] //when i try to empty it, i get this exception 
*AssertionError: Collection was loaded during event 
handling. *//though the action is getting 
performed, putting a try catch is solving it, but why is it coming?

though,
g.packages=[]this is working fine with no exception



Thanks for any kind of help, in advance

-- 
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] Re: insertion into association table, giving integrityerror

2017-04-10 Thread mike bayer



On 04/10/2017 03:46 AM, shrey.chau...@invicto.in wrote:

A package object is getting created, and its inserting it into database,
have to debug why is it inserting into db

Somehow i got it working, added: db.session.rollback(), before append
statement
Dont know whether its the right approach


it's not.  you need to identify where that Package object is getting 
generated.


If it were me, I'd be using pdb, however, if you want a more high level 
approach, you can use the transient_to_pending event to intercept where 
a Package object is unexpectedly getting attached:


http://docs.sqlalchemy.org/en/rel_1_1/orm/session_events.html#transient-to-pending








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.


[sqlalchemy] Re: insertion into association table, giving integrityerror

2017-04-10 Thread shrey . chauhan
A package object is getting created, and its inserting it into database, 
have to debug why is it inserting into db

Somehow i got it working, added: db.session.rollback(), before append 
statement
Dont know whether its the right approach

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.


Re: [sqlalchemy] Re: insertion into association table, giving integrityerror

2017-04-09 Thread mike bayer



On 04/09/2017 02:18 PM, shrey.chau...@invicto.in wrote:

Traceback (most recent call last):
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask/app.py",
line 1982, in wsgi_app
response = self.full_dispatch_request()
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask/app.py",
line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask/app.py",
line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask/app.py",
line 1612, in full_dispatch_request
rv = self.dispatch_request()
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask/app.py",
line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask_limiter/extension.py",
line 442, in __inner
return obj(*a, **k)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask/views.py",
line 84, in view
return self.dispatch_request(*args, **kwargs)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask/views.py",
line 149, in dispatch_request
return meth(*args, **kwargs)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/api/v2_0/groups.py",
line 119, in patch
db.session.commit()
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py",
line 157, in do
return getattr(self.registry(), name)(*args, **kwargs)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
line 874, in commit
self.transaction.commit()
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
line 461, in commit
self._prepare_impl()
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
line 441, in _prepare_impl
self.session.flush()
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
line 2139, in flush
self._flush(objects)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
line 2259, in _flush
transaction.rollback(_capture_exception=True)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py",
line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
line 2223, in _flush
flush_context.execute()
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py",
line 389, in execute
rec.execute(self)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py",
line 548, in execute
uow
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py",
line 181, in save_obj
mapper, table, insert)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py",
line 835, in _emit_insert_statements
execute(statement, params)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
line 945, in execute
return meth(self, multiparams, params)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/sql/elements.py",
line 263, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
line 1053, in _execute_clauseelement
compiled_sql, distilled_params
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
line 1189, in _execute_context
context)
  File
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
line 1394, in _handle_dbapi_exception
exc_info
  File

[sqlalchemy] Re: insertion into association table, giving integrityerror

2017-04-09 Thread shrey . chauhan
Traceback (most recent call last):
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask/app.py",
 
line 1982, in wsgi_app
response = self.full_dispatch_request()
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask/app.py",
 
line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask/app.py",
 
line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask/app.py",
 
line 1612, in full_dispatch_request
rv = self.dispatch_request()
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask/app.py",
 
line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask_limiter/extension.py",
 
line 442, in __inner
return obj(*a, **k)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask/views.py",
 
line 84, in view
return self.dispatch_request(*args, **kwargs)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/flask/views.py",
 
line 149, in dispatch_request
return meth(*args, **kwargs)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/api/v2_0/groups.py",
 
line 119, in patch
db.session.commit()
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py",
 
line 157, in do
return getattr(self.registry(), name)(*args, **kwargs)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
 
line 874, in commit
self.transaction.commit()
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
 
line 461, in commit
self._prepare_impl()
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
 
line 441, in _prepare_impl
self.session.flush()
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
 
line 2139, in flush
self._flush(objects)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
 
line 2259, in _flush
transaction.rollback(_capture_exception=True)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py",
 
line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py",
 
line 2223, in _flush
flush_context.execute()
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py",
 
line 389, in execute
rec.execute(self)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py",
 
line 548, in execute
uow
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py",
 
line 181, in save_obj
mapper, table, insert)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py",
 
line 835, in _emit_insert_statements
execute(statement, params)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
 
line 945, in execute
return meth(self, multiparams, params)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/sql/elements.py",
 
line 263, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
 
line 1053, in _execute_clauseelement
compiled_sql, distilled_params
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
 
line 1189, in _execute_context
context)
  File 
"/home/schauhan/NewOrchestrator/session_broker/session_broker/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
 
line 1394, in _handle_dbapi_exception
exc_info
  File