[sqlalchemy] UniqueConstraint in __table_args__ in inherited models

2016-11-29 Thread TomS.

Hi,

When I use __table_args__ like it is in the below example following 
warning appears:


Column 'person_name' on table [...] being replaced by Column [...] which 
has the same key.  Consider use_labels for select() statements.


I know that it can be solved by adding __table_args__ to inherited 
models, but maybe there is more elegant way of doing it (and a way to 
avoid pasting same line)?


class PPLTest(db.Model):

__abstract__ = True

person_pk_id = db.Column(db.Integer, primary_key=True)
person_name = db.Column(db.String(1000))

__table_args__ = (db.UniqueConstraint(u'person_name'), 
{u'mysql_engine': u'InnoDB'})


class Level1(PPLTest):

__tablename__ = u'ppl_level1'
__bind_key__ = u'test'

class Level2(PPLTest):

__tablename__ = u'ppl_level2'
__bind_key__ = u'test'

Cheers and thank you!

--
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] Using Python 'sorted' doesn't change data?

2016-11-15 Thread TomS.

Great! Thank you for your answer and hints!

Cheers


On 11/08/2016 10:30 PM, mike bayer wrote:
it does not, and as an exercise I'd recommend trying to theorize how 
sorted() *could* make such an effect - and if it did, what assumptions 
would it be making?   Hopefully this would reveal that the assumptions 
the library would need to make in order to even make such a thing 
happen would not be appropriate (e.g., what column would it be 
changing?  how would it know to change *that* column and not another 
one?  what if your query.all() were against some other SQL statement?  
etc).




On 11/08/2016 12:58 PM, TomS. wrote:

Hi,

I know this is silly question, but I just need confirmation - Python
'sorted' doesn't affect in any way data stored in DB?

Example:

class PPL(db.Model):

person_pk_id = db.Column(db.Integer, primary_key=True)
person_type = db.Column(db.Integer)
person_order_s = db.Column(db.Integer)
person_order_k = db.Column(db.Integer)

all_ppl = PPL.query.all()

technician = [person for person in all_ppl if person.person_type == 1]

technician_sorted = sorted(technician, key=lambda k: getattr(k,
u'person_order_' + extra_par))

Using 'sorted' won't make any changes in the rows which are in table
used by PPL?

Thank you very much!

Cheers





--
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] Using Python 'sorted' doesn't change data?

2016-11-08 Thread TomS.

Hi,

I know this is silly question, but I just need confirmation - Python 
'sorted' doesn't affect in any way data stored in DB?


Example:

class PPL(db.Model):

person_pk_id = db.Column(db.Integer, primary_key=True)
person_type = db.Column(db.Integer)
person_order_s = db.Column(db.Integer)
person_order_k = db.Column(db.Integer)

all_ppl = PPL.query.all()

technician = [person for person in all_ppl if person.person_type == 1]

technician_sorted = sorted(technician, key=lambda k: getattr(k, 
u'person_order_' + extra_par))


Using 'sorted' won't make any changes in the rows which are in table 
used by PPL?


Thank you very much!

Cheers

--
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] Properly definition of Many To One/One to Many without FK

2016-09-06 Thread TomS.

Hi,

In my model I am not allowed to use FK. I've been trying to figure out 
how one to many/many to one relationship should be properly defined or 
in which class to be sure that it works as expected. I've recently used 
very similar example - here it is:


class DepTest(db.Model):

__tablename__ = u'deptest'
__bind_key__ = u'section'

department_pk_id = db.Column(db.Integer, primary_key=True)
department_id = db.Column(db.Integer)
department_name = db.Column(db.String(1000))

# @declared_attr
# def ppl(clss):
# return relationship(u'Level1',
# primaryjoin=u'and_(Level1.department_id==DepTest.department_id,'
# u' 
Level1.person_include_flag==1)',

# foreign_keys=[clss.department_id],
# backref=u'department')


class PPLTest(db.Model):

__abstract__ = True

person_pk_id = db.Column(db.Integer, primary_key=True)
person_name = db.Column(db.String(1000))
person_surname = db.Column(db.String(1000))
person_include_flag = db.Column(db.Integer)
department_id = db.Column(db.Integer)

@hybrid_property
def person_name_surname(self):
return self.person_name + u' ' + self.person_surname


class Level1(PPLTest):

__tablename__ = u'ppl_level1'
__bind_key__ = u'section'

# @declared_attr
# def department(clss):
# return relationship(u'DepTest',
# primaryjoin=u'and_(Level1.department_id==DepTest.department_id,'
# u' 
Level1.person_include_flag==1)',

# foreign_keys=[clss.department_id],
# backref=u'ppl')

I am looking for the comprehensive answer to be sure that result is not 
coincidental.


If @declared_attr part in DepTest is uncommented (while in Level1 is 
commented), I get only one person/scalar (although there are more ppl) 
after running:


q = DepTest.query.all()
print q[0].ppl

and list here:

q = Level1.query.all()
print q[1].department

In case if @declared_attr part in Level1 is uncommented (while in 
DepTest is commented) it behaves according to my intentions:


# list
q = DepTest.query.all()
print q[0].ppl

# scalar
q = Level1.query.all()
print q[1].department

In docs the difference between 1 to many, many to 1 depends on (if I 
understand it correctly) in which class ForeignKey is defined (because 
usage of backref creates bidirectional behaviour). So what is the rule 
in the cases without FK? Should @declared_attr be put in class which 
represents "many" entities (thus get ppl as a list).


Any links to doc, info would be very helpful.

--
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] Inherited abstract model with relationship causing "Cannot compile Column object until its 'name' is assigned."

2016-09-02 Thread TomS.

On 09/02/2016 04:37 PM, Mike Bayer wrote:



On 09/02/2016 10:25 AM, TomS. wrote:


On 08/30/2016 08:25 PM, Mike Bayer wrote:



On 08/30/2016 01:54 PM, TomS. wrote:

Hi,

I would like to create an abstract model which should be inherited.
Unfortunately, I am not allowed to used Foreign Keys, this is the 
reason

why I introduced work around via relationship. Here is the whole code:

class DepTest(db.Model):

__tablename__ = u'deptest'
__bind_key__ = u'section'

department_pk_id = db.Column(db.Integer, primary_key=True)
department_id = db.Column(db.Integer)
department_name = db.Column(db.String(1000))


class PPLTest(db.Model):

__abstract__ = True

person_pk_id = db.Column(db.Integer, primary_key=True)
person_name = db.Column(db.String(1000))
person_surname = db.Column(db.String(1000))
department_id = db.Column(db.Integer)

@hybrid_property
def person_name_surname(self):
return self.person_name + u' ' + self.person_surname


class Level1(PPLTest):

__tablename__ = u'ppl_level1'
__bind_key__ = u'section'

department = relationship(
DepTest,
primaryjoin=remote(DepTest.department_id) == foreign(
PPLTest.department_id))


But I get "Cannot compile Column object until its 'name' is assigned."

So I defined department (in Level1) using @declared_attr:

def department(clss):
return relationship(DepTest,
primaryjoin=lambda:
remote(DepTest.department_id) == foreign(
clss.department_id)
)

and now it works as expected. The question is - is it defined 
properly,

or I should be aware of sth?


Yes, using a lambda here, or alternatively using a string-eval, is a
good way to defer evaluation of that primaryjoin until declarative has
set things up and put names in your Column objects.

Just curious to see the stack trace here because I don't see where the
Column needs to actually be compiled into a string.

Thank you!

Flask==0.10.1
Flask-SQLAlchemy==2.0
SQLAlchemy==1.0.14

Here it is:



OH.  ha, weird.  OK, that was it trying to raise a different error, 
"Could not locate any simple equality expressions "\

"involving locally mapped foreign key columns for "\
"%s join condition "\
"'%s' on relationship %s."

But that failed because it couldn't turn the join condition into a 
string.


But w/ lambda you don't get that error either huh ?

It happens when Level1 is defined this way:

department = relationship(
DepTest,
primaryjoin=remote(DepTest.department_id) == foreign(
PPLTest.department_id))

# @declared_attr
# def department(clss):
# return relationship(DepTest,
# primaryjoin=lambda: 
remote(DepTest.department_id) == foreign(

# clss.department_id)
# )

It is ok when defintion is as follows (with or without lambda):

# department = relationship(
# DepTest,
# primaryjoin=remote(DepTest.department_id) == foreign(
# PPLTest.department_id))

@declared_attr
def department(clss):
return relationship(DepTest,
primaryjoin=lambda: 
remote(DepTest.department_id) == foreign(

clss.department_id)
)








Traceback (most recent call last):
  File "testing.py", line 20, in 
q = testm.Level1.query.all()
  File
"/test/_env/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py",
line 452, in __get__
mapper = orm.class_mapper(type)
  File "/test/_env/lib/python2.7/site-packages/sqlalchemy/orm/base.py",
line 421, in class_mapper
mapper = _inspect_mapped_class(class_, configure=configure)
  File "/test/_env/lib/python2.7/site-packages/sqlalchemy/orm/base.py",
line 400, in _inspect_mapped_class
mapper._configure_all()
  File
"/test/_env/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line
1167, in _configure_all
configure_mappers()
  File
"/test/_env/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line
2770, in configure_mappers
mapper._post_configure_properties()
  File
"/test/_env/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line
1710, in _post_configure_properties
prop.init()
  File
"/test/_env/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py",
line 183, in init
self.do_init()
  File
"/test/_env/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", 
line

1629, in do_init
self._setup_join_conditions()
  File
"/test/_env/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", 
line

1704, in _setup_join_conditions
can_be_synced_fn=self._columns_are_mapped
  File
"/test/_env/lib/pyt

[sqlalchemy] Inherited abstract model with relationship causing "Cannot compile Column object until its 'name' is assigned."

2016-08-30 Thread TomS.

Hi,

I would like to create an abstract model which should be inherited. 
Unfortunately, I am not allowed to used Foreign Keys, this is the reason 
why I introduced work around via relationship. Here is the whole code:


class DepTest(db.Model):

__tablename__ = u'deptest'
__bind_key__ = u'section'

department_pk_id = db.Column(db.Integer, primary_key=True)
department_id = db.Column(db.Integer)
department_name = db.Column(db.String(1000))


class PPLTest(db.Model):

__abstract__ = True

person_pk_id = db.Column(db.Integer, primary_key=True)
person_name = db.Column(db.String(1000))
person_surname = db.Column(db.String(1000))
department_id = db.Column(db.Integer)

@hybrid_property
def person_name_surname(self):
return self.person_name + u' ' + self.person_surname


class Level1(PPLTest):

__tablename__ = u'ppl_level1'
__bind_key__ = u'section'

department = relationship(
DepTest,
primaryjoin=remote(DepTest.department_id) == foreign(
PPLTest.department_id))


But I get "Cannot compile Column object until its 'name' is assigned."

So I defined department (in Level1) using @declared_attr:

def department(clss):
return relationship(DepTest,
primaryjoin=lambda: 
remote(DepTest.department_id) == foreign(

clss.department_id)
)

and now it works as expected. The question is - is it defined properly, 
or I should be aware of sth?


Thank you for any suggestions!

Cheers

--
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] SAWarning shows not immediately, but after some time of app execution

2016-08-02 Thread TomS.


On 07/22/2016 05:13 PM, Simon King wrote:

On Fri, Jul 22, 2016 at 3:25 PM, TomS. <pidev...@gmail.com> wrote:

On 07/19/2016 06:41 PM, Mike Bayer wrote:



On 07/19/2016 11:51 AM, TomS. wrote:

Hi,

We have Flask app which uses SQLAlchemy. Weird error started to happen
recently. The difficulty is that we can't reproduce the error (/figure
out conditions causing issue) - maybe someone could help. Any hints/tips
would be appreciated.

There is a part in the code which constructs IN in SQL:

MyModel.id.in_(my_ids)

For some cases my_ids is an empty list. It works without any problems,
but after some time the same query (using empty list) starts to raise an
exception:

SAWarning: The IN-predicate on "MyModel.id" was invoked with an empty
sequence. This results in a contradiction, which nonetheless can be
expensive to evaluate.  Consider alternative strategies for improved
performance.

After restarting app, everything works again.

The question is - why this exception is not risen always (although we
tried to run app with empty list directly), but after some time of app
execution (~1 day)?


It's not an exception, it's a warning.  Python warnings by default emit only
once, see:
https://docs.python.org/2/library/warnings.html#the-warnings-filter

Ok, this is the explanation why it doesn't show up regularly. Thank you.

I don't know why SAWarning is treated as error. Here is the log:

2016-07-21 13:58:14,108 ERROR: Exception on /own [GET]
Traceback (most recent call last):
[...]
   File
"/home/developer/.virtualenvs/rest/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py",
line 1297, in warn
 warnings.warn(msg, exc.SAWarning, stacklevel=2)
SAWarning: The IN-predicate on "MyModel.id" was invoked with an empty
sequence. This results in a contradiction, which nonetheless can be
expensive to evaluate.

I will also ask on Flask group, but maybe you know the reason - Flask config
regarding logging is as follow:

import logging

from logging.handlers import RotatingFileHandler

logger_file_handler = RotatingFileHandler('my.log', maxBytes=1024 * 1024 *
100, backupCount=20)
logger_file_handler.setLevel(logging.DEBUG)
logger_formatter = logging.Formatter(u'%(asctime)s %(levelname)s:
%(message)s')

logger_file_handler.setFormatter(logger_formatter)

logging.captureWarnings(True)

app.logger.addHandler(logger_file_handler)
app.logger.setLevel(logging.DEBUG)

There is no code which change behavior (by  filter) of SAWarning to 'error'
...

The python warnings system is completely separate from the logging
system. Whether or not a particular warning is turned into an
exception is driven by the "warnings filter":

https://docs.python.org/2/library/warnings.html#the-warnings-filter

I think you must be configuring the warnings filter somewhere, because
by default this shouldn't raise an exception.
Yes, you were right. I finally found the source of the issue. There was 
link in the part of the code which set warnings to behave like the 
exceptions. But this code was not executed always but under certain 
conditions. That was the first issue with debuging. The second one - the 
fact that it is web app which is run using many processes. So on some 
"process" code was already executed while on others not.


Thank you for the ideas!


Simon


Cheers!

--
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] SAWarning shows not immediately, but after some time of app execution

2016-07-22 Thread TomS.

On 07/19/2016 06:41 PM, Mike Bayer wrote:



On 07/19/2016 11:51 AM, TomS. wrote:

Hi,

We have Flask app which uses SQLAlchemy. Weird error started to happen
recently. The difficulty is that we can't reproduce the error (/figure
out conditions causing issue) - maybe someone could help. Any hints/tips
would be appreciated.

There is a part in the code which constructs IN in SQL:

MyModel.id.in_(my_ids)

For some cases my_ids is an empty list. It works without any problems,
but after some time the same query (using empty list) starts to raise an
exception:

SAWarning: The IN-predicate on "MyModel.id" was invoked with an empty
sequence. This results in a contradiction, which nonetheless can be
expensive to evaluate.  Consider alternative strategies for improved
performance.

After restarting app, everything works again.

The question is - why this exception is not risen always (although we
tried to run app with empty list directly), but after some time of app
execution (~1 day)?


It's not an exception, it's a warning.  Python warnings by default 
emit only once, see: 
https://docs.python.org/2/library/warnings.html#the-warnings-filter

Ok, this is the explanation why it doesn't show up regularly. Thank you.

I don't know why SAWarning is treated as error. Here is the log:

2016-07-21 13:58:14,108 ERROR: Exception on /own [GET]
Traceback (most recent call last):
[...]
  File 
"/home/developer/.virtualenvs/rest/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", 
line 1297, in warn

warnings.warn(msg, exc.SAWarning, stacklevel=2)
SAWarning: The IN-predicate on "MyModel.id" was invoked with an empty 
sequence. This results in a contradiction, which nonetheless can be 
expensive to evaluate.


I will also ask on Flask group, but maybe you know the reason - Flask 
config regarding logging is as follow:


import logging

from logging.handlers import RotatingFileHandler

logger_file_handler = RotatingFileHandler('my.log', maxBytes=1024 * 1024 
* 100, backupCount=20)

logger_file_handler.setLevel(logging.DEBUG)
logger_formatter = logging.Formatter(u'%(asctime)s %(levelname)s: 
%(message)s')


logger_file_handler.setFormatter(logger_formatter)

logging.captureWarnings(True)

app.logger.addHandler(logger_file_handler)
app.logger.setLevel(logging.DEBUG)

There is no code which change behavior (by filter) of SAWarning to 
'error' ...


Cheers






Details:
Flask==0.10.1
Flask-SQLAlchemy==2.1
SQLAlchemy==1.0.14
MySQL DB

Cheers






--
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] SAWarning shows not immediately, but after some time of app execution

2016-07-19 Thread TomS.
my_ids = [] is initialized in the body of static method (on the top of 
it) which is run via Flask's route decorator.


But I can't understand why following code:

my_ids = []
MyModel.id.in_(my_ids)

doesn't raise SAWarning if 'an empty sequence' causes warning.

Cheers


On 07/19/2016 06:14 PM, Антонио Антуан wrote:


Looks like `my_ids` become empty 'after some time of app execution'. 
How do you initialize the variable?



вт, 19 июля 2016 г., 18:51 TomS. <pidev...@gmail.com 
<mailto:pidev...@gmail.com>>:


Hi,

We have Flask app which uses SQLAlchemy. Weird error started to happen
recently. The difficulty is that we can't reproduce the error (/figure
out conditions causing issue) - maybe someone could help. Any
hints/tips
would be appreciated.

There is a part in the code which constructs IN in SQL:

MyModel.id.in_(my_ids)

For some cases my_ids is an empty list. It works without any problems,
but after some time the same query (using empty list) starts to
raise an
exception:

SAWarning: The IN-predicate on "MyModel.id" was invoked with an empty
sequence. This results in a contradiction, which nonetheless can be
expensive to evaluate.  Consider alternative strategies for improved
performance.

After restarting app, everything works again.

The question is - why this exception is not risen always (although we
tried to run app with empty list directly), but after some time of app
execution (~1 day)?

Details:
Flask==0.10.1
Flask-SQLAlchemy==2.1
SQLAlchemy==1.0.14
MySQL DB

Cheers


--
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
<mailto:sqlalchemy%2bunsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--

Антон

--
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 
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com 
<mailto:sqlalchemy@googlegroups.com>.

Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


--
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] SAWarning shows not immediately, but after some time of app execution

2016-07-19 Thread TomS.

Hi,

We have Flask app which uses SQLAlchemy. Weird error started to happen 
recently. The difficulty is that we can't reproduce the error (/figure 
out conditions causing issue) - maybe someone could help. Any hints/tips 
would be appreciated.


There is a part in the code which constructs IN in SQL:

MyModel.id.in_(my_ids)

For some cases my_ids is an empty list. It works without any problems, 
but after some time the same query (using empty list) starts to raise an 
exception:


SAWarning: The IN-predicate on "MyModel.id" was invoked with an empty 
sequence. This results in a contradiction, which nonetheless can be 
expensive to evaluate.  Consider alternative strategies for improved 
performance.


After restarting app, everything works again.

The question is - why this exception is not risen always (although we 
tried to run app with empty list directly), but after some time of app 
execution (~1 day)?


Details:
Flask==0.10.1
Flask-SQLAlchemy==2.1
SQLAlchemy==1.0.14
MySQL DB

Cheers


--
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] Too slow commit?

2016-03-25 Thread TomS.

Great - I've implemented mentioned pattern and it works!

Thank you!

On 03/22/2016 07:10 PM, Jonathan Vanasco wrote:

Yes, the approach would be to use the exact same session:

 dbSession = SQLSession.sql_session()
 functionA()
 functionB()
dbSession.close()

It looks like functionA and functionB each call 
`SQLSession.sql_session()`, which will cause problems. I believe that 
will create multiple sessions, which will have different data and may 
cause blocking and transactional issues.


I usually write my celery tasks like this:

 def task_foo():
 dbSession =  new_session()
 functionA(dbSession)
 functionB(dbSession)
dbSession.close()



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


--
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] Too slow commit?

2016-03-22 Thread TomS.

Thank you for your answer!

Function B is called after A. Literally:
/
//functionA()//
//functionB()/

It works fine when at the end of A /inserting_session.close_all()/ is 
called, but it is not very elegant (moreover it probably causes "MySQL 
server has gone away", but this is a different story).


Cheers!

On 03/22/2016 06:01 PM, Simon King wrote:
It sounds like the transaction for task B is starting before A's 
transaction has been committed, but you haven't really given enough 
information to debug further. How are you managing your sessions and 
transactions? Do B and A actually overlap (ie. does B start before A 
finishes)?


Simon

On Tue, Mar 22, 2016 at 4:07 PM, TomS. <pidev...@gmail.com 
<mailto:pidev...@gmail.com>> wrote:


Could you advise what would be the best approach for the following
problem.

I have Flask-Celery task. Task consists of two functions (both are
run in the same one Celery task): A and B. Function A calculates
values which are then used by B function. Values are stored in DB
(via SQLAlchemy). The problem is that, somehow, values from A are
commited too slow (?), so function B has nothing to calc (whole
task has to be called once again to get results from function B).

I use below pattern:

New values inside function A are commited this way:

/try://
//
//inserting_session.add(new_value)//
//
//inserting_session.commit()//
//
//except Exception as e://
//
//inserting_session.rollback()//
//
//and after all values are worked out://
//
//inserting_session.close()/


Where:

/class SQLSession(object)://
//
//sql_engine = None//
//
//@staticmethod//
//def sql_session()://
//
//if SQLSession.sql_engine is None://
//
//# create engine//
//
//SQLSession.sql_engine = create_engine([...]),//
//pool_recycle=30//
//)//
//
//# create a configured "Session" class//
//
//insering_session_maker =
sessionmaker(bind=SQLSession.sql_engine)//
//
//# scope session//
//
//inserting_session = scoped_session(insering_session_maker)//
//
//# create a Session//
//
//inserting_session_scoped = inserting_session()//
//
//return inserting_session_scoped//
//
//
//inserting_session = SQLSession.sql_session()/


It works when instead of
/
//inserting_session.close()/

close_all() is called:

/inserting_session.close()/

But it probably is not the best solution.

The question is - how to deal with this issue?

Cheers!
-- 
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
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


--
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 
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com 
<mailto:sqlalchemy@googlegroups.com>.

Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


--
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] Too slow commit?

2016-03-22 Thread TomS.

Could you advise what would be the best approach for the following problem.

I have Flask-Celery task. Task consists of two functions (both are run 
in the same one Celery task): A and B. Function A calculates values 
which are then used by B function. Values are stored in DB (via 
SQLAlchemy). The problem is that, somehow, values from A are commited 
too slow (?), so function B has nothing to calc (whole task has to be 
called once again to get results from function B).


I use below pattern:

New values inside function A are commited this way:

/try://
//
//inserting_session.add(new_value)//
//
//inserting_session.commit()//
//
//except Exception as e://
//
//inserting_session.rollback()//
//
//and after all values are worked out://
//
//inserting_session.close()/


Where:

/class SQLSession(object)://
//
//sql_engine = None//
//
//@staticmethod//
//def sql_session()://
//
//if SQLSession.sql_engine is None://
//
//# create engine//
//
//SQLSession.sql_engine = create_engine([...]),//
//pool_recycle=30//
//)//
//
//# create a configured "Session" class//
//
//insering_session_maker = 
sessionmaker(bind=SQLSession.sql_engine)//

//
//# scope session//
//
//inserting_session = scoped_session(insering_session_maker)//
//
//# create a Session//
//
//inserting_session_scoped = inserting_session()//
//
//return inserting_session_scoped//
//
//
//inserting_session = SQLSession.sql_session()/


It works when instead of
/
//inserting_session.close()/

close_all() is called:

/inserting_session.close()/

But it probably is not the best solution.

The question is - how to deal with this issue?

Cheers!

--
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] @property mapping via hybrid property?

2016-02-29 Thread TomS.

Hi!

I've got an answer on Stack ( 
http://stackoverflow.com/questions/35653889/sqlalchemy-property-mapping/35654405 
) - solution uses relationship and an association proxy. But I also 
found out that it can be achieved via hybrid property. How to do this?


I have following models:

|class  Details(db.Model):

details_id=  db.Column(db.Integer,  primary_key=True)
details_main=  db.Column(db.String(50))
details_desc=  db.Column(db.String(50))

class  Data(db.Model):

data_id=  db.Column(db.Integer,  primary_key=True)
data_date=  db.Column(db.Date)
details_main=  db.Column(db.String(50))

@property
def  details_desc(self):

result=  object_session(self).\
scalar(
select([Details.details_desc]).
where(Details.details_main==  self.details_main)
)

return  result|


Now, I would like to run query using filter which depends on defined 
property. I get an empty results (of course proper data is in DB). It 
doesn't work because, probably, I have to map this property. The 
question is how to do this? (One limitation: FK are not allowed in this 
DB's design).


|Data.query\
.filter(Data.details_desc==  unicode('test'))\
.all()|



Cheers,
TomS

Exported from Notepad++

--
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] How to cast/convert inserting data using type of column

2015-12-18 Thread TomS.

Hi,

I've tried on Stack, but without positive results, thus maybe someone 
here faced similar issue and can help:).


I would like to store XML results in DB. There are many tags (in example 
I only used two) in XML content, so instead of addressing every column's 
name explicitly, I iterate over returned XML's tags, check if name of 
tag equals name of the column -> if so, value for particular column is set.


The question is - how to deal with type? Do I have to convert content of 
XML's tag in |setattr| (if so - how to do it?) or sqlAlchemy does it 
automagically during insertion and I don't have to consider this issue?


Here is pseudo-code:

XML:

| DDFAGHFAA -345.22098312 |

Python:

|class  MyModel(db.Model):

prop_ident=  db.Column(db.String(20))
prop_value=  db.Column(db.Float)

lxml_res=  xml_res.xpath(u'//parts')

mymodel_inst=  MyModel()

for  resin  lxml_res:

if  hasattr(mymodel_inst,  (u'prop_'  +  res.tag).lower()):

setattr(mymodel_inst,  (u'prop_'  +  res.tag).lower(),  res.text)|

I found that it can be accomplished using |isinstance| and type of 
column, but maybe there is simpler solution (or it is done by sqlAlchemy).


Cheers,
TomS
<http://stackoverflow.com/questions/4165143/easy-convert-betwen-sqlalchemy-columns-and-data-types>

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