Re: [sqlalchemy] how can i get .engine.execute result?

2014-07-03 Thread 'Frank Liou' via sqlalchemy


> Hi Simon


thanks for your answer

but i can't understand what's "fetchall()" mean

i remove the  " fetchall()"  it still work

 

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Automatically set cascade settings based on "ON DELETE" / "ON UPDATE" when reflecting?

2014-07-03 Thread Paul Molodowitch
I wasn't advocating making this connection "in general" (though I like the
autoconfigure option!), but only specifically for the case of reflection -
in this case, we know the DB supports it, and it would result in a better
python interface to the already existing tables.


On Thu, Jul 3, 2014 at 3:20 PM, Mike Bayer  wrote:

>
> On 7/3/14, 6:15 PM, Mike Bayer wrote:
> > On 7/3/14, 5:45 PM, Paul Molodowitch wrote:
> >> I noticed that sqlalchemy now properly sets the onpudate / ondelete
> >> properties of foreign keys when reflecting tables:
> >>
> >>
> https://bitbucket.org/zzzeek/sqlalchemy/issue/2183/support-on-delete-update-in-foreign-key
> >>
> >> However, it doesn't seem to set the cascade properties of
> >> relationships to reflect these properties. ie, if the Child table
> >> references the Parent table with a foreign key that has "ON DELETE
> >> CASCADE", and the reference column does not allow NULL, when you
> >> delete a parent table that has children, you will get an error,
> >> because sqlalchemy will try to set the child's ref to NULL.
> >>
> >> ideally we should add "delete" in the relationship's cascade
> >> properties (and probably delete-orphan as well), and then set
> >> passive_updates=True.
> >>
> >> Or am I missing something obvious  / doing something wrong / etc?
> > the configuration of a Column or ForeignKey has never been directly
> > linked to how relationship() gets configured.   passive_updates in
> > particular is a thorny one as not every database supports ON UPDATE
> > CASCADE, but for that matter not every database even supports ON DELETE
> > CASCADE.   There's also lots of variants to ON UPDATE and ON DELETE and
> > SQLAlchemy has no awareness of any of these directly.
> >
> > If we were to explore some automatic configuration of relationship based
> > on these attributes of ForeignKey, it would take place within the
> > automap extension: see
> > http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/automap.html.
> >
> > There are also recipes such that both relationship() and ForeignKey()
> > are generated at once, these are also good places for this kind of thing
> > to happen.  See
> >
> https://bitbucket.org/zzzeek/pycon2014_atmcraft/src/a6d96575bc497ce0c952bb81db9c05d054c98bb5/atmcraft/model/meta/orm.py?at=master
> > for an example of this, I still am thinking of a way recipes like this
> > could also be integrated into SQLAlchemy, possibly as an enhancement to
> > declarative.
>
> or a flag like "autoconfigure=True" on relationship().   this would also
> set up innerjoin=True for joined eager loading if the FK is not null.
> if the primaryjoin condition is too complex (has mulitple FKs),
> autoconfigure would raise an exception.
>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "sqlalchemy" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/sqlalchemy/WaVTCpBOVPk/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at http://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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Automatically set cascade settings based on "ON DELETE" / "ON UPDATE" when reflecting?

2014-07-03 Thread Mike Bayer

On 7/3/14, 6:15 PM, Mike Bayer wrote:
> On 7/3/14, 5:45 PM, Paul Molodowitch wrote:
>> I noticed that sqlalchemy now properly sets the onpudate / ondelete
>> properties of foreign keys when reflecting tables:
>>
>> https://bitbucket.org/zzzeek/sqlalchemy/issue/2183/support-on-delete-update-in-foreign-key
>>
>> However, it doesn't seem to set the cascade properties of
>> relationships to reflect these properties. ie, if the Child table
>> references the Parent table with a foreign key that has "ON DELETE
>> CASCADE", and the reference column does not allow NULL, when you
>> delete a parent table that has children, you will get an error,
>> because sqlalchemy will try to set the child's ref to NULL.
>>
>> ideally we should add "delete" in the relationship's cascade
>> properties (and probably delete-orphan as well), and then set
>> passive_updates=True.
>>
>> Or am I missing something obvious  / doing something wrong / etc?
> the configuration of a Column or ForeignKey has never been directly
> linked to how relationship() gets configured.   passive_updates in
> particular is a thorny one as not every database supports ON UPDATE
> CASCADE, but for that matter not every database even supports ON DELETE
> CASCADE.   There's also lots of variants to ON UPDATE and ON DELETE and
> SQLAlchemy has no awareness of any of these directly.
>
> If we were to explore some automatic configuration of relationship based
> on these attributes of ForeignKey, it would take place within the
> automap extension: see
> http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/automap.html. 
>
> There are also recipes such that both relationship() and ForeignKey()
> are generated at once, these are also good places for this kind of thing
> to happen.  See
> https://bitbucket.org/zzzeek/pycon2014_atmcraft/src/a6d96575bc497ce0c952bb81db9c05d054c98bb5/atmcraft/model/meta/orm.py?at=master
> for an example of this, I still am thinking of a way recipes like this
> could also be integrated into SQLAlchemy, possibly as an enhancement to
> declarative.

or a flag like "autoconfigure=True" on relationship().   this would also
set up innerjoin=True for joined eager loading if the FK is not null.  
if the primaryjoin condition is too complex (has mulitple FKs),
autoconfigure would raise an exception.


-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Automatically set cascade settings based on "ON DELETE" / "ON UPDATE" when reflecting?

2014-07-03 Thread Mike Bayer

On 7/3/14, 5:45 PM, Paul Molodowitch wrote:
> I noticed that sqlalchemy now properly sets the onpudate / ondelete
> properties of foreign keys when reflecting tables:
>
> https://bitbucket.org/zzzeek/sqlalchemy/issue/2183/support-on-delete-update-in-foreign-key
>
> However, it doesn't seem to set the cascade properties of
> relationships to reflect these properties. ie, if the Child table
> references the Parent table with a foreign key that has "ON DELETE
> CASCADE", and the reference column does not allow NULL, when you
> delete a parent table that has children, you will get an error,
> because sqlalchemy will try to set the child's ref to NULL.
>
> ideally we should add "delete" in the relationship's cascade
> properties (and probably delete-orphan as well), and then set
> passive_updates=True.
>
> Or am I missing something obvious  / doing something wrong / etc?

the configuration of a Column or ForeignKey has never been directly
linked to how relationship() gets configured.   passive_updates in
particular is a thorny one as not every database supports ON UPDATE
CASCADE, but for that matter not every database even supports ON DELETE
CASCADE.   There's also lots of variants to ON UPDATE and ON DELETE and
SQLAlchemy has no awareness of any of these directly.

If we were to explore some automatic configuration of relationship based
on these attributes of ForeignKey, it would take place within the
automap extension: see
http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/automap.html. 

There are also recipes such that both relationship() and ForeignKey()
are generated at once, these are also good places for this kind of thing
to happen.  See
https://bitbucket.org/zzzeek/pycon2014_atmcraft/src/a6d96575bc497ce0c952bb81db9c05d054c98bb5/atmcraft/model/meta/orm.py?at=master
for an example of this, I still am thinking of a way recipes like this
could also be integrated into SQLAlchemy, possibly as an enhancement to
declarative.


-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Automatically set cascade settings based on "ON DELETE" / "ON UPDATE" when reflecting?

2014-07-03 Thread Paul Molodowitch
I noticed that sqlalchemy now properly sets the onpudate / ondelete 
properties of foreign keys when reflecting tables:

https://bitbucket.org/zzzeek/sqlalchemy/issue/2183/support-on-delete-update-in-foreign-key

However, it doesn't seem to set the cascade properties of relationships to 
reflect these properties. ie, if the Child table references the Parent 
table with a foreign key that has "ON DELETE CASCADE", and the reference 
column does not allow NULL, when you delete a parent table that has 
children, you will get an error, because sqlalchemy will try to set the 
child's ref to NULL.

ideally we should add "delete" in the relationship's cascade properties 
(and probably delete-orphan as well), and then set passive_updates=True.

Or am I missing something obvious  / doing something wrong / etc?

- Paul

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] "failed to locate a name" error when using model with relationship

2014-07-03 Thread trustedbox
Muchos grasias, Michael!

Issue resolved.
There were a couple of other ForeignKey issues, seems to be okay now.
So far I understand that there is a lot to learn for me, so I was wondering 
- do you by chance accept apprentices ?

I would be willing to help writing fixes/minor features whatever for an 
exchange of your guidance. What do you say?

No matter what, thank you again for assistance in these issues. I'm now 
going to test and fix everything else :-)

Andrey

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] "failed to locate a name" error when using model with relationship

2014-07-03 Thread Mike Bayer
OK thats great but you have a long way to go:

import cycle:

classic$ .venv/bin/python
Python 2.7.5 (default, Mar  7 2014, 19:17:16)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import model
Traceback (most recent call last):
  File "", line 1, in 
  File "model/__init__.py", line 1, in 
from app import db
  File "app/__init__.py", line 46, in 
import views
  File "/Users/classic/Desktop/tmp/artFlask/views.py", line 4, in 
from api.artists import Artists
  File "/Users/classic/Desktop/tmp/artFlask/api/artists.py", line 32, in

from utils.app_ctx import ApplicationContext
  File "/Users/classic/Desktop/tmp/artFlask/utils/app_ctx.py", line 1,
in 
from model import Artwork
ImportError: cannot import name Artwork
>>>



tests dont run:

$ .venv/bin/nosetests -v
Failure: ImportError (No module named mail) ... ERROR
Failure: ImportError (No module named app) ... ERROR
Failure: ImportError (No module named app) ... ERROR

==
ERROR: Failure: ImportError (No module named mail)
--
Traceback (most recent call last):
  File
"/Users/classic/Desktop/tmp/artFlask/.venv/lib/python2.7/site-packages/nose/loader.py",
line 414, in loadTestsFromName
addr.filename, addr.module)
  File
"/Users/classic/Desktop/tmp/artFlask/.venv/lib/python2.7/site-packages/nose/importer.py",
line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
  File
"/Users/classic/Desktop/tmp/artFlask/.venv/lib/python2.7/site-packages/nose/importer.py",
line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
  File "/Users/classic/Desktop/tmp/artFlask/app/__init__.py", line 6, in

from mail import mail
ImportError: No module named mail

etc.

These are all very basic Python issues.  If you can get on IRC and get
someone to help you get your program just to import in a basic way you
can probably figure out why your models aren't initializing.Good luck!









On 7/3/14, 3:29 PM, trusted...@gmail.com wrote:
> Hi Mike!
>
> I've updated requirements. Let me check if I didn't commit something.
>
> четверг, 3 июля 2014 г., 20:41:55 UTC+3 пользователь Michael Bayer
> написал:
>
> to start with, you have an import cycle. fix that first.
>
> classics-MacBook-Pro-2:artFlask classic$ .venv/bin/python
> Python 2.7.5 (default, Mar  7 2014, 19:17:16)
> [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import model
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "model/__init__.py", line 1, in 
> from app import db
>   File "app/__init__.py", line 46, in 
> import views
>   File "/Users/classic/Desktop/tmp/artFlask/views.py", line 4, in
> 
> from api.artists import Artists
>   File "/Users/classic/Desktop/tmp/artFlask/api/artists.py", line
> 32, in 
> from utils.app_ctx import ApplicationContext
>   File "/Users/classic/Desktop/tmp/artFlask/utils/app_ctx.py",
> line 1, in 
> from model import Artwork
> ImportError: cannot import name Artwork
>
>
> well fix the package part first.  then fix the import cycle.
>
>
>
>
>
> On 7/3/14, 1:39 PM, Mike Bayer wrote:
>> this project needs:
>>
>> 1. a package
>>
>> 2. a setup.py file
>>
>> 3. a requirements.txt file
>>
>>
>> I am running in each dependency indivdually as I get import
>> errors.  very painful.
>>
>>
>>
>>
>> On 7/3/14, 1:19 PM, trust...@gmail.com  wrote:
>>> Mike, here is the source for the models:
>>> https://github.com/ArtAPI/artFlask/blob/refactoring/model/__init__.py
>>> 
>>>
>>> I would really apprieciate your feedback on this, I'm really new
>>> to flask and SQLAlchemy.
>>> The other source files (artList.py etc) are also there.
>>>
>>> Thank you,
>>> Andrey
>>>
>>> четверг, 3 июля 2014 г., 17:33:09 UTC+3 пользователь Michael
>>> Bayer написал:
>>>
>>>
>>> On 7/3/14, 3:23 AM, trust...@gmail.com wrote:
>>> > Hi, Michael,
>>> >
>>> > here is the stack trace:
>>> >   File
>>> "/home/andrey/projects/art/artFlask/api/artList.py", line 30,
>>> > in post
>>> > item = app_ctx.create_item_from_context()
>>> >   File
>>> "/home/andrey/projects/art/artFlask/utils/app_ctx.py", line 53,
>>> > in create_item_from_context
>>> > item = ModelClass(**data)
>>> >   File "", line 2, in __init__
>>>
>>> this means on line 30 of your artList.py class, the "Person"
>>> class
>>> hasn't been set up yet, or was set up and failed to do 

Re: [sqlalchemy] Reuse pre-defined Enum?

2014-07-03 Thread Mike Bayer
this works fine for me:

def upgrade():

mytype = ENUM('a', 'b', 'c', create_type=False, name='myenum')

mytype.create(op.get_bind(), checkfirst=False)

op.create_table('t1',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('col1', mytype)
)

op.create_table('t2',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('col1', mytype)
)



output:

INFO  [alembic.migration] Running upgrade None -> 47bed260c052, initial rev
INFO  [sqlalchemy.engine.base.Engine]
SELECT EXISTS (
SELECT * FROM pg_catalog.pg_type t
WHERE t.typname = %(typname)s
AND pg_type_is_visible(t.oid)
)
   
INFO  [sqlalchemy.engine.base.Engine] {'typname': u'myenum'}
INFO  [sqlalchemy.engine.base.Engine] CREATE TYPE myenum AS ENUM ('a',
'b', 'c')
INFO  [sqlalchemy.engine.base.Engine] {}
INFO  [sqlalchemy.engine.base.Engine]
CREATE TABLE t1 (
id INTEGER NOT NULL,
col1 myenum
)


INFO  [sqlalchemy.engine.base.Engine] {}
INFO  [sqlalchemy.engine.base.Engine]
CREATE TABLE t2 (
id INTEGER NOT NULL,
col1 myenum
)


INFO  [sqlalchemy.engine.base.Engine] {}
INFO  [sqlalchemy.engine.base.Engine] INSERT INTO alembic_version
(version_num) VALUES ('47bed260c052')
INFO  [sqlalchemy.engine.base.Engine] {}
INFO  [sqlalchemy.engine.base.Engine] COMMIT

works with --sql too, output:

CREATE TYPE myenum AS ENUM ('a', 'b', 'c');

CREATE TABLE t1 (
id INTEGER NOT NULL,
col1 myenum
);

CREATE TABLE t2 (
id INTEGER NOT NULL,
col1 myenum
);

INSERT INTO alembic_version (version_num) VALUES ('47bed260c052');

COMMIT;





On 7/3/14, 3:23 PM, Anton wrote:
> Hey guys,
>
> I am facing the same issue, I am using PostgreSQL and want to use
> native ENUM type in two tables. For migration I am going to use alembic.
> Did you guys find out any was to do this?
>
> Thanks,
> Anton
>
> On Thursday, May 1, 2014 10:33:21 AM UTC-7, Michael Bayer wrote:
>
> OK.  ENUM is something I’d have to dedicate several days of
> attention on :(
>
>
> On May 1, 2014, at 1:32 PM, Vlad Wing  > wrote:
>
>> Yes, that's exactly what happend. I specified "create_type=False"
>> and it was ignored. Alembic tried to create the type anyway and,
>> of course, it failed.
>>
>> -- 
>> 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+...@googlegroups.com .
>> To post to this group, send email to sqlal...@googlegroups.com
>> .
>> Visit this group at http://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 http://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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] "failed to locate a name" error when using model with relationship

2014-07-03 Thread trustedbox
Hi Mike!

I've updated requirements. Let me check if I didn't commit something.

четверг, 3 июля 2014 г., 20:41:55 UTC+3 пользователь Michael Bayer написал:
>
>  to start with, you have an import cycle. fix that first.
>
> classics-MacBook-Pro-2:artFlask classic$ .venv/bin/python 
> Python 2.7.5 (default, Mar  7 2014, 19:17:16) 
> [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import model
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "model/__init__.py", line 1, in 
> from app import db
>   File "app/__init__.py", line 46, in 
> import views
>   File "/Users/classic/Desktop/tmp/artFlask/views.py", line 4, in 
> from api.artists import Artists
>   File "/Users/classic/Desktop/tmp/artFlask/api/artists.py", line 32, in 
> 
> from utils.app_ctx import ApplicationContext
>   File "/Users/classic/Desktop/tmp/artFlask/utils/app_ctx.py", line 1, in 
> 
> from model import Artwork
> ImportError: cannot import name Artwork
>
>
> well fix the package part first.  then fix the import cycle.
>
>
>
>
>
> On 7/3/14, 1:39 PM, Mike Bayer wrote:
>  
> this project needs:
>
> 1. a package
>
> 2. a setup.py file
>
> 3. a requirements.txt file
>
>
> I am running in each dependency indivdually as I get import errors.  very 
> painful.
>
>
>
>
> On 7/3/14, 1:19 PM, trust...@gmail.com  wrote:
>  
> Mike, here is the source for the models: 
> https://github.com/ArtAPI/artFlask/blob/refactoring/model/__init__.py
>
>  I would really apprieciate your feedback on this, I'm really new to 
> flask and SQLAlchemy.
> The other source files (artList.py etc) are also there.
>
>  Thank you,
> Andrey
>
> четверг, 3 июля 2014 г., 17:33:09 UTC+3 пользователь Michael Bayer 
> написал: 
>>
>>
>> On 7/3/14, 3:23 AM, trust...@gmail.com wrote: 
>> > Hi, Michael, 
>> > 
>> > here is the stack trace: 
>> >   File "/home/andrey/projects/art/artFlask/api/artList.py", line 30, 
>> > in post 
>> > item = app_ctx.create_item_from_context() 
>> >   File "/home/andrey/projects/art/artFlask/utils/app_ctx.py", line 53, 
>> > in create_item_from_context 
>> > item = ModelClass(**data) 
>> >   File "", line 2, in __init__ 
>>
>> this means on line 30 of your artList.py class, the "Person" class 
>> hasn't been set up yet, or was set up and failed to do so and for some 
>> reason the error didn't get reported (such as if some unusual system of 
>> loading modules were present). 
>>
>>
>>   -- 
> 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+...@googlegroups.com .
> To post to this group, send email to sqlal...@googlegroups.com 
> .
> Visit this group at http://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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Reuse pre-defined Enum?

2014-07-03 Thread Anton
Hey guys,

I am facing the same issue, I am using PostgreSQL and want to use native 
ENUM type in two tables. For migration I am going to use alembic. 
Did you guys find out any was to do this?

Thanks,
Anton

On Thursday, May 1, 2014 10:33:21 AM UTC-7, Michael Bayer wrote:
>
> OK.  ENUM is something I’d have to dedicate several days of attention on :(
>
>
> On May 1, 2014, at 1:32 PM, Vlad Wing > 
> wrote:
>
> Yes, that's exactly what happend. I specified "create_type=False" and it 
> was ignored. Alembic tried to create the type anyway and, of course, it 
> failed.
>
> -- 
> 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+...@googlegroups.com .
> To post to this group, send email to sqlal...@googlegroups.com 
> .
> Visit this group at http://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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] "failed to locate a name" error when using model with relationship

2014-07-03 Thread Mike Bayer
this project needs:

1. a package

2. a setup.py file

3. a requirements.txt file


I am running in each dependency indivdually as I get import errors. 
very painful.




On 7/3/14, 1:19 PM, trusted...@gmail.com wrote:
> Mike, here is the source for the models:
> https://github.com/ArtAPI/artFlask/blob/refactoring/model/__init__.py
>
> I would really apprieciate your feedback on this, I'm really new to
> flask and SQLAlchemy.
> The other source files (artList.py etc) are also there.
>
> Thank you,
> Andrey
>
> четверг, 3 июля 2014 г., 17:33:09 UTC+3 пользователь Michael Bayer
> написал:
>
>
> On 7/3/14, 3:23 AM, trust...@gmail.com  wrote:
> > Hi, Michael,
> >
> > here is the stack trace:
> >   File "/home/andrey/projects/art/artFlask/api/artList.py", line
> 30,
> > in post
> > item = app_ctx.create_item_from_context()
> >   File "/home/andrey/projects/art/artFlask/utils/app_ctx.py",
> line 53,
> > in create_item_from_context
> > item = ModelClass(**data)
> >   File "", line 2, in __init__
>
> this means on line 30 of your artList.py class, the "Person" class
> hasn't been set up yet, or was set up and failed to do so and for
> some
> reason the error didn't get reported (such as if some unusual
> system of
> loading modules were present).
>
>
> -- 
> 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 http://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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] "failed to locate a name" error when using model with relationship

2014-07-03 Thread Mike Bayer
to start with, you have an import cycle. fix that first.

classics-MacBook-Pro-2:artFlask classic$ .venv/bin/python
Python 2.7.5 (default, Mar  7 2014, 19:17:16)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import model
Traceback (most recent call last):
  File "", line 1, in 
  File "model/__init__.py", line 1, in 
from app import db
  File "app/__init__.py", line 46, in 
import views
  File "/Users/classic/Desktop/tmp/artFlask/views.py", line 4, in 
from api.artists import Artists
  File "/Users/classic/Desktop/tmp/artFlask/api/artists.py", line 32, in

from utils.app_ctx import ApplicationContext
  File "/Users/classic/Desktop/tmp/artFlask/utils/app_ctx.py", line 1,
in 
from model import Artwork
ImportError: cannot import name Artwork


well fix the package part first.  then fix the import cycle.





On 7/3/14, 1:39 PM, Mike Bayer wrote:
> this project needs:
>
> 1. a package
>
> 2. a setup.py file
>
> 3. a requirements.txt file
>
>
> I am running in each dependency indivdually as I get import errors. 
> very painful.
>
>
>
>
> On 7/3/14, 1:19 PM, trusted...@gmail.com wrote:
>> Mike, here is the source for the models:
>> https://github.com/ArtAPI/artFlask/blob/refactoring/model/__init__.py
>>
>> I would really apprieciate your feedback on this, I'm really new to
>> flask and SQLAlchemy.
>> The other source files (artList.py etc) are also there.
>>
>> Thank you,
>> Andrey
>>
>> четверг, 3 июля 2014 г., 17:33:09 UTC+3 пользователь Michael Bayer
>> написал:
>>
>>
>> On 7/3/14, 3:23 AM, trust...@gmail.com  wrote:
>> > Hi, Michael,
>> >
>> > here is the stack trace:
>> >   File "/home/andrey/projects/art/artFlask/api/artList.py",
>> line 30,
>> > in post
>> > item = app_ctx.create_item_from_context()
>> >   File "/home/andrey/projects/art/artFlask/utils/app_ctx.py",
>> line 53,
>> > in create_item_from_context
>> > item = ModelClass(**data)
>> >   File "", line 2, in __init__
>>
>> this means on line 30 of your artList.py class, the "Person" class
>> hasn't been set up yet, or was set up and failed to do so and for
>> some
>> reason the error didn't get reported (such as if some unusual
>> system of
>> loading modules were present).
>>
>>
>> -- 
>> 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 http://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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Automatically set primary key to None when deleted?

2014-07-03 Thread Paul Molodowitch
>
> Particularly since sqlalchemy has already established that it's willing to
> expire dict members when they may not be valid anymore - ie, what it does
> to clear any "cached" values from a row proxy after the session is
> committed.
>
> well it doesn't expire the deleted object right now because it's been
> evicted from the Session by the time the commit goes to expire things.
> Changing that behavior now would definitely bite a lot of people who depend
> on how it is right now (other people who are also looking at their deleted
> objects against my recommendations... :) )
>

Makes sense - maybe it could be a configurable option? Dunno how many
people (besides me!) would be interested in such behavior, though... =P

> from sqlalchemy import inspect
>> def exists(session, obj):
>> state = inspect(obj)
>> return session.query(state.mapper).get(state.identity) is None
>>
>> print exists(sess, a1)
>>
>
>  Hmm... very interesting.  I'll have to read up what what exactly this is
> doing (ie, what is state.identity?)...
>
> it's documented here:
> http://docs.sqlalchemy.org/en/rel_0_9/orm/internals.html?highlight=instancestate#sqlalchemy.orm.state.InstanceState.identity
>
>
Thanks for the link!

 the unique constraints are a set though.   not necessarily deterministic
which one it would locate first.  I'd use more of some kind of declared
system on the class:

>
>  Not clear on why this matters - if we're iterating through all the
> constraints, and returning True if any of them is matched, what difference
> does it make which one is evaluated first?  Except potentially from a
> performance standpoint, I suppose...
>
> what if there are two constraints, and only one is satisfied for a given
> object's values (e.g. the constraint is now satisfied by some other row),
> the other one is not present.  Is the answer True or False?
>

In the scenario I was envisioning, True (ie, it exists).  Basically, "Would
it violate ANY unique constraints if I tried to insert it? Yes".

Of course, I see your point: that in some situations, this might not fit
conceptually with the answer to the question, "Does THIS object exist in
the database?"  But I guess that's likely your point... that there isn't
really a good "universal" way to answer that question.

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Automatically set primary key to None when deleted?

2014-07-03 Thread Mike Bayer

On 7/3/14, 1:01 PM, Paul Molodowitch wrote:
>
>
> That makes sense... but if if it really means nothing, and we
> shouldn't be looking at it, then why keep it's attributes around at all? 
because it is an additional step to actually erase the attributes and
just hadn't been considered.


> Particularly since sqlalchemy has already established that it's
> willing to expire dict members when they may not be valid anymore -
> ie, what it does to clear any "cached" values from a row proxy after
> the session is committed.
well it doesn't expire the deleted object right now because it's been
evicted from the Session by the time the commit goes to expire
things. Changing that behavior now would definitely bite a lot of
people who depend on how it is right now (other people who are also
looking at their deleted objects against my recommendations... :) )




>
> Of course, you could make the case that other pieces of the program
> may want to inspect the data that was on there, after the fact...
> maybe you're going to print out something that says, "RIP Steggy", or
> something - but in that case, the one field that really DOESN'T make
> any sense in this case (and it seems like it would be a common
> pattern!) is the one that exists solely as a means to look it up in
> the database, it's auto-incremented id column.  Which is what prompted
> this question...

well all the cols don't exist anymore, not just the primary key. the
inspect(obj).deleted call does allow this information (that the object
was deleted) to be known, though not very transparently.

> from sqlalchemy import inspect
> def exists(session, obj):
> state = inspect(obj)
> return session.query(state.mapper).get(state.identity) is None
>
> print exists(sess, a1)
>
>
> Hmm... very interesting.  I'll have to read up what what exactly this
> is doing (ie, what is state.identity?)...
it's documented here:
http://docs.sqlalchemy.org/en/rel_0_9/orm/internals.html?highlight=instancestate#sqlalchemy.orm.state.InstanceState.identity

 
>
> from sqlalchemy import inspect, UniqueConstraint
> def exists(session, obj):
> state = inspect(obj)
> table = state.mapper.local_table
> for const in table.constraints:
> if isinstance(const, UniqueConstraint):
>crit = and_(*[col == getattr(obj, col.key) for col in
> const])
>return session.query(state.mapper).filter(crit).count() > 0
> else:
>return False
>
>
> Yep, it looks like that's doing basically what I was thinking of.  Thanks!
>
> the unique constraints are a set though.   not necessarily
> deterministic which one it would locate first.  I'd use more of
> some kind of declared system on the class:
>
>
> Not clear on why this matters - if we're iterating through all the
> constraints, and returning True if any of them is matched, what
> difference does it make which one is evaluated first?  Except
> potentially from a performance standpoint, I suppose...
what if there are two constraints, and only one is satisfied for a given
object's values (e.g. the constraint is now satisfied by some other
row), the other one is not present.  Is the answer True or False?  


-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] "failed to locate a name" error when using model with relationship

2014-07-03 Thread trustedbox
Mike, here is the source for the models:
https://github.com/ArtAPI/artFlask/blob/refactoring/model/__init__.py

I would really apprieciate your feedback on this, I'm really new to flask 
and SQLAlchemy.
The other source files (artList.py etc) are also there.

Thank you,
Andrey

четверг, 3 июля 2014 г., 17:33:09 UTC+3 пользователь Michael Bayer написал:
>
>
> On 7/3/14, 3:23 AM, trust...@gmail.com  wrote: 
> > Hi, Michael, 
> > 
> > here is the stack trace: 
> >   File "/home/andrey/projects/art/artFlask/api/artList.py", line 30, 
> > in post 
> > item = app_ctx.create_item_from_context() 
> >   File "/home/andrey/projects/art/artFlask/utils/app_ctx.py", line 53, 
> > in create_item_from_context 
> > item = ModelClass(**data) 
> >   File "", line 2, in __init__ 
>
> this means on line 30 of your artList.py class, the "Person" class 
> hasn't been set up yet, or was set up and failed to do so and for some 
> reason the error didn't get reported (such as if some unusual system of 
> loading modules were present). 
>
>
>

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Automatically set primary key to None when deleted?

2014-07-03 Thread Paul Molodowitch
On Wed, Jul 2, 2014 at 9:22 PM, Mike Bayer  wrote:

>
> On 7/2/14, 10:05 PM, Paul Molodowitch wrote:
>
>  Suppose I have a super simple table like this:
>
>   class Dinosaur(Base):
>  __tablename__ = 'dinosaurs'
>  id = Column(Integer, primary_key=True)
>  name = Column(String(255))
>
>
>  We assume that the id is set up in such a way that by default it always
> gets a unique value - ie, it uses autoincrement in MySQL, or a sequence in
> postgres, etc.
>
>  Now, suppose I get an instance of this class, and then delete it:
>
>   steggy = session.query(Dinosaur).filter_by(name='Steggy').one()
>  print steggy.id
>  session.delete(steggy)
>  session.commit()
>  print steggy.id
>
>
>  What I'd ideally like to see is that it first print the id of the row
> that it pulled from the database, and then print 'None':
>
>  30
> None
>
>
>  Is there any way that I can configure the id column / property so that
> it is automatically "cleared" on delete like this?
>
>
> the "steggy" object is a proxy for a database row.  when you delete that
> row, then commit the transaction, the object is detached from the session,
> and everything on it is expired.  there is no row.  check
> inspect(steggy).deleted, it will say True - that means in your system, the
> object is meaningless.  ideally no part of your program would be looking at
> that proxy any more, you should throw it away.  it means nothing.
>

That makes sense... but if if it really means nothing, and we shouldn't be
looking at it, then why keep it's attributes around at all?  Particularly
since sqlalchemy has already established that it's willing to expire dict
members when they may not be valid anymore - ie, what it does to clear any
"cached" values from a row proxy after the session is committed.

Of course, you could make the case that other pieces of the program may
want to inspect the data that was on there, after the fact... maybe you're
going to print out something that says, "RIP Steggy", or something - but in
that case, the one field that really DOESN'T make any sense in this case
(and it seems like it would be a common pattern!) is the one that exists
solely as a means to look it up in the database, it's auto-incremented id
column.  Which is what prompted this question...


If not, as a consolation prize, I'd also be interested in the easiest way
> to query if a given instance exists in the database - ie, I could do
> something like:
>
>
>  session.exists(steggy)
>
>
>  OR
>
>  steggy.exists()
>
>
>
> from sqlalchemy import inspect
> def exists(session, obj):
> state = inspect(obj)
> return session.query(state.mapper).get(state.identity) is None
>
> print exists(sess, a1)
>

Hmm... very interesting.  I'll have to read up what what exactly this is
doing (ie, what is state.identity?)... It's possibly that
inspect(steggy).deleted may just give me what I need though. Thanks for
both those tips! (In case you couldn't tell, I'm still new to / exploring
sqlalchemy...)


> ...which, in this case, would simply run a query to see if any dinosaurs
> exist with the name "Steggy".
>
> that's totally different.  That's a WHERE criterion on the "name" field,
> which is not the primary key.  that's something specific to your class
> there.
>

True.  There's really no way for a generic "exists" function to know what
conditions you want to query a generic class on to determine "existence."
 Which is why I was suggesting the uniqueness constraint...

 Needing to set up some extra parameters to make this possible - such as
adding a unique constraint on the name column -

> OK, so you want a function that a. receives an object b. looks for UNIQUE
> constraints on it c. queries by those unique constraints (I guess you want
> the first one present?  not clear.  a table can have a lot of unique
> constraints on it) that would be:
>

Sort of - the thinking here was that you could just ask, "If I tried to
insert this object into the table, would it violate any uniqueness
constraints?", and get back a boolean result... and you could use that as a
reasonable heuristic for determining existence, in a fairly generic way.


> from sqlalchemy import inspect, UniqueConstraint
> def exists(session, obj):
> state = inspect(obj)
> table = state.mapper.local_table
> for const in table.constraints:
> if isinstance(const, UniqueConstraint):
>crit = and_(*[col == getattr(obj, col.key) for col in const])
>return session.query(state.mapper).filter(crit).count() > 0
> else:
>return False
>

Yep, it looks like that's doing basically what I was thinking of.  Thanks!

the unique constraints are a set though.   not necessarily deterministic
> which one it would locate first.  I'd use more of some kind of declared
> system on the class:
>

Not clear on why this matters - if we're iterating through all the
constraints, and returning True if any of them is matched, what difference
does it make which one is evaluated first?  Ex

Re: [sqlalchemy] "failed to locate a name" error when using model with relationship

2014-07-03 Thread trustedbox
Adding import instruction to artList.py didn't seem to help:
from model import Artwork, Person

What else can I do ?

четверг, 3 июля 2014 г., 17:33:09 UTC+3 пользователь Michael Bayer написал:
>
>
> On 7/3/14, 3:23 AM, trust...@gmail.com  wrote: 
> > Hi, Michael, 
> > 
> > here is the stack trace: 
> >   File "/home/andrey/projects/art/artFlask/api/artList.py", line 30, 
> > in post 
> > item = app_ctx.create_item_from_context() 
> >   File "/home/andrey/projects/art/artFlask/utils/app_ctx.py", line 53, 
> > in create_item_from_context 
> > item = ModelClass(**data) 
> >   File "", line 2, in __init__ 
>
> this means on line 30 of your artList.py class, the "Person" class 
> hasn't been set up yet, or was set up and failed to do so and for some 
> reason the error didn't get reported (such as if some unusual system of 
> loading modules were present). 
>
>
>

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] @as_declarative() weirdness

2014-07-03 Thread Roland van Laar
Hello,

I'm trying to override __new__ method on the declarative base.

Calling super(Base, cls).__new__ actually calls itself.
Something is happening with the mro, but I'm not quite sure what and how to 
proceed.

At the bottom of this email is a small example that demonstrates the 
problem.

Regards,

Roland



from sqlalchemy.ext.declarative import as_declarativefrom sqlalchemy import 
Column, Integer
@as_declarative()class Base(object):

def __new__(cls, *args, **kwargs):
return super(Base, cls).__new__(cls, *args, **kwargs)

class Test(Base):
__tablename__ = 'test'
id = Column(Integer, primary_key=True)
Test()  # RuntimeError: maximum recursion depth exceeded while calling a Python 
object


-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] can I have update_all in sqlalchemy

2014-07-03 Thread Atul Motade
HI 
I am using pyramid framework with sqlalchemy , I want to update 100 
different column with different values.
is there any function available like add_all to update a table(ex 
update_all ) in sqlalchemy.

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] how to query one-to-many relationships, in a complicated way?

2014-07-03 Thread Mike Bayer

On 7/2/14, 9:24 AM, Chung WONG wrote:
> Hi list,
>
> For this problem I am even having trouble think of a proper subject
> for it.
> I try my best to express as clear as possible and sorry for any
> confusions.
>
> Say there are three classes with relationship defined as below: 
> class User(Base):
> __tablename__ = 'users'
> id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
>
> class Feedback(Base):
> __tablename__ = 'feedbacks'
> id = Column(Integer, Sequence('feedback_id_seq'), primary_key=True)
> service_id = Column(Integer, ForeignKey('services.id'))
> score=Column(Integer)
>
> service=relationship('Service', backref=backref("feedbacks"))
>
> class Service(Base):
> __tablename__ = 'services'
> id = Column(Integer, Sequence('service_id_seq'), primary_key=True)
> provider_id = Column(Integer, ForeignKey('users.id'))
> requester_id = Column(Integer, ForeignKey('users.id'))
>
> provider = relationship('User', foreign_keys=provider_id,
> backref='services_received')
> requester = relationship('User', foreign_keys=requester_id,
> backref='services_sent')
>
>
> *User* and *Service* is a one to many relationship, and a user can be
> a Service provider or requester.
>
> For *Service* and *Feedback, *it is a one to many relationship too. A
> requester can give a score to the Service.
>
> *The question is, how can I get the sum(scores) of all services for a
> user?*
> *
> *
> I thought I could do sth like:
> #provider is a given object
> *provider.services_recieved.feedbacks*
> but it threw error:
> *AttributeError: 'InstrumentedList' object has no attribute 'feedbacks'*

there's no query(x.y.z.bar) feature in the SQLAlchemy ORM.  To join
across entities, use join(), this is first introduced in the ORM
tutorial at
http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#querying-with-joins.

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] "failed to locate a name" error when using model with relationship

2014-07-03 Thread Mike Bayer

On 7/3/14, 3:23 AM, trusted...@gmail.com wrote:
> Hi, Michael,
>
> here is the stack trace:
>   File "/home/andrey/projects/art/artFlask/api/artList.py", line 30,
> in post
> item = app_ctx.create_item_from_context()
>   File "/home/andrey/projects/art/artFlask/utils/app_ctx.py", line 53,
> in create_item_from_context
> item = ModelClass(**data)
>   File "", line 2, in __init__

this means on line 30 of your artList.py class, the "Person" class
hasn't been set up yet, or was set up and failed to do so and for some
reason the error didn't get reported (such as if some unusual system of
loading modules were present).


-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] defaultdict functionality for association proxies

2014-07-03 Thread Brian Findlay
Ok, Mike. Thanks.

Ended up removing the trigger from the model and pushing that code to the 
controller, so that "foo" updates the "bar" attribute after form data with 
UserCourse objects is handled.


On Wednesday, July 2, 2014 9:42:11 PM UTC-4, Michael Bayer wrote:
>
>
> On 7/2/14, 2:59 PM, Brian Findlay wrote: 
> > Mike, thanks for the response. 
> > 
> > (1) "foo" updates a particular User attribute based on a calculation 
> > performed on the user.courses collection. I'm listening for the "set" 
> > event on UserCourse objects to trigger "foo" to update that User 
> > attribute, but that isn't working with new users because -- as you say 
> > -- the "set" event is called before the actual attribute association 
> > occurs. What is the first event I could listen for that would 
> > recognize the new attribute association? 
> in that event I only see "grade" being set so it's not clear to me what 
> the bigger picture is.  If this is all within the association proxy 
> setup and within when a new UserCourse is created, I'd have to step 
> through w/ pdb to see when things happen, but often with these assoc 
> proxy cases, building out a custom proxier that does the things you want 
> is often necessary if you really want sophisticated behaviors. 
>
> we don't really have a solution to the attribute events being before the 
> thing is set.  adding all new "after set" events isn't possible without 
> adding even more latency, and attribute mutation operations are already 
> a huge performance bottleneck.   Association proxies and attribute 
> events are both handy but they only go so far in their capabilities.   
>
>
>

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] how can i get .engine.execute result?

2014-07-03 Thread Simon King
On Thu, Jul 3, 2014 at 8:48 AM, 'Frank Liou' via sqlalchemy
 wrote:
> def get_query():
> conn = engine.connect()
> check = 'SELECT * FROM friends'
> obj = [conn.execute(check)]
> jqs = json.dumps(obj)
> return jqs
>
>
>
> result is
>
> Internal Server Error
>
> The server encountered an internal error and was unable to complete your
> request. Either the server is overloaded or there is an error in the
> application.
>
>
> if i change obj = '123'
>
> it will show 123
>
>
> but when i use execute to query
>
> it will no request
>
> how can that be?
>
> is it wrong type to catch?
>
>
> pleaese help me .thanks
>

Firstly, when you get a message like that from your web server, you
can normally find the full error message in the web server logs. The
location of those depends on how the server has been set up, but they
are usually somewhere like /var/log/httpd/error_log or
/var/log/apache2/error_log on Linux.

If you looked in there, I think you would probably see a stack trace
ending with a message about being unable to convert a ResultProxy to
json. After this:

obj = [conn.execute(check)]

"obj" is now a list containing a single element, which is a
ResultProxy object. You probably wanted something like this instead:

obj = conn.execute(check).fetchall()

After running that, obj will now be a list of rows from the database.
However, you still won't be able to convert it directly to json,
because each row is a RowProxy object, and the json library doesn't
know how to handle RowProxy objects.

The simplest thing would probably be to convert each row to a dictionary:

rows = conn.execute(check).fetchall()
obj = [dict(row.items()) for row in rows]

Hope that helps,

Simon

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] how can i get .engine.execute result?

2014-07-03 Thread 'Frank Liou' via sqlalchemy
def get_query():
conn = engine.connect()
check = 'SELECT * FROM friends'
obj = [conn.execute(check)]
jqs = json.dumps(obj)
return jqs



result is 
 Internal Server Error 

The server encountered an internal error and was unable to complete your 
request. Either the server is overloaded or there is an error in the 
application.


if i change obj = '123'

it will show 123


but when i use execute to query

it will no request

how can that be?

is it wrong type to catch?


pleaese help me .thanks

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] "failed to locate a name" error when using model with relationship

2014-07-03 Thread trustedbox
Hi, Michael,

here is the stack trace:
  File "/home/andrey/projects/art/artFlask/api/artList.py", line 30, in post
item = app_ctx.create_item_from_context()
  File "/home/andrey/projects/art/artFlask/utils/app_ctx.py", line 53, in 
create_item_from_context
item = ModelClass(**data)
  File "", line 2, in __init__

  File 
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py",
 
line 322, in _new_state_if_none
state = self._state_constructor(instance, self)
  File 
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py",
 
line 712, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
  File 
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py",
 
line 155, in _state_constructor
self.dispatch.first_init(self, self.class_)
  File 
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/event/attr.py",
 
line 257, in __call__
fn(*args, **kw)
  File 
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
 
line 2673, in _event_on_first_init
configure_mappers()
  File 
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
 
line 2569, in configure_mappers
mapper._post_configure_properties()
  File 
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py",
 
line 1682, in _post_configure_properties
prop.init()
  File 
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py",
 
line 143, in init
self.do_init()
  File 
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py",
 
line 1510, in do_init
self._process_dependent_arguments()
  File 
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py",
 
line 1567, in _process_dependent_arguments
self.target = self.mapper.mapped_table
  File 
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py",
 
line 712, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
  File 
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py",
 
line 1484, in mapper
argument = self.argument()
  File 
"/home/andrey/envs/art_flask2/lib/python2.7/site-packages/sqlalchemy/ext/declarative/clsregistry.py",
 
line 275, in __call__
(self.prop.parent, self.arg, n.args[0], self.cls)
InvalidRequestError: When initializing mapper Mapper|Artwork|artwork, 
expression 'Person' failed to locate a name ("name 'Person' is not 
defined"). If this is a class name, consider adding this relationship() to 
the  class after both dependent classes have been 
defined.

Does it tell you anything ?

среда, 2 июля 2014 г., 17:38:55 UTC+3 пользователь Michael Bayer написал:
>
>  
> On 7/2/14, 10:21 AM, trust...@gmail.com  wrote:
>  
> Hi Michael, thank you for the answer. 
> Both classes are in the same file so I don't see how it could be possible 
> that one class is used while other is not imported.
> Could you help me with that ?
>  
>
> that would mean you're doing something that is invoking Artwork as a 
> mapped class and causing the mapper config step to occur before it gets 
> down to Person.   The stack trace here would show exactly where that 
> originates.
>
>
>
>
>
>  
>  Andrey
>
> вторник, 1 июля 2014 г., 21:05:11 UTC+3 пользователь Michael Bayer 
> написал: 
>>
>>  
>> On 7/1/14, 1:17 PM, trust...@gmail.com wrote:
>>  
>>  I have two classes, Artwork and Person. Artwork has a relationship to 
>> Person.
>>
>> However, when I try to use them, I get an error thrown:
>>
>> InvalidRequestError: When initializing mapper Mapper|Artwork|artwork, 
>>> expression 'Person' failed to locate a name ("name 'Person' is not 
>>> defined"). If this is a class name, consider adding this relationship() to 
>>> the  class after both dependent classes have been 
>>> defined.
>>
>>
>>  
>>  Here are the classes themselves, defined in model/__init__.py
>>
>> class Artwork(db.Model, SimpleSerializeMixin):
>> id = db.Column(db.Integer, primary_key=True)
>> 
>> artist_id = db.Column(db.String(256), db.ForeignKey('person.sub'))
>> artist = db.relationship('Person', backref='artworks')
>>
>>  class Person(db.Model, SimpleSerializeMixin):
>> sub = db.Column(db.String(256), primary_key=True)
>>
>>  
>>  
>>  I checked with debugger and in sqlalchemy/ext/declarative/clsregistry.py 
>> (_class_resolver.__call__())
>> there is a line:
>> x = eval(self.arg, globals(), self._dict)
>>  No "Person" or "Artwork" or any other class defined in the file are 
>> present in globals(). self._dict is empty
>> So it fails with an NameError exception.
>>
>>  What could be the issue ?
>>  
>>
>> it's usually that the Person code wasn't run, e.g. that the module in 
>> which it is located was not imported, before you tried to use the Artwork 
>> class.  All the