[sqlalchemy] How to test if an object attribute is a relationship

2018-09-06 Thread Jacek Blocki
Is there a stock function to test if an object attribute is a relationship? 
I have created below function to test it, is there a better solution?

def is_relationship(obj, attr):
try:
p = inspect(getattr(type(obj), attr)).property
except (AttributeError, NoInspectionAvailable):
return False
if isinstance(p, RelationshipProperty):
return True
return False


-- 
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: online vs offline produce different "python stack traces" and general question

2018-09-06 Thread HP3
Thank you very much Mike!

The older baseline is on v0.9.7 but I am not sure if I can bump it up 
'blindly' ... I think all constructs are "vanilla" so maybe it's safe.

In my v0.9.7 based model, I used history_meta recipe (slightly modified 
though).

At any rate, after fudging around with some operations (aka commenting 
things out, moving things after others) I managed to produce an offline 
.sql migration that when ran within a single transaction did fail exactly 
as the online migration did. Same exact DB error: "cannot drop table 
groups_history because ..."

At this point, I think I will continue fudging around with the order of 
things and eventually be able interject the bulk_updates/bulk_inserts I 
need.

I'll report back with results after bumping up from 0.9.7 to see if the 
error goes away.

Again, thanks a lot

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy-alembic+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: online vs offline produce different "python stack traces" and general question

2018-09-06 Thread Mike Bayer
On Thu, Sep 6, 2018 at 1:14 PM, HP3  wrote:
> Hello,
>
> As I try alembic (for the first time), I ran into the following stack traces
> when attempting to conduct a "nasty" migration.
>
> I call it "nasty" because the original model was based on sqlalchemy v0.9 +
> python 2.7 and the new model is sqlalchemy v1.2 + python 3.6.
> ON TOP OF THAT, it's a massive table and model restructuring. Backend is
> postgres.
>
> I am thinking that I can leverage alembic to do the heavy lifting of
> creating/dropping stuff while I can interject my own bulk_inserts and
> bulk_updates and preserve the existing data.  Something like:
>
> // versions/deadbeef_nasty.py
>
> def upgrade():
> op.drop_table(...)
> op.drop_table(...)
> ...
>
> # Here is where I am planning to add my bulk_inserts and bulk_updates
>
> op.add_column(...)
> op.drop_column(...)
> ...
>
>
>
> Is this a reasonable approach?

yes


>
> With regards to the stack traces, they differ between online (first stack)
> and offline (second). Hence, this post.
> Does that make sense?


youre getting two totally different error conditions.  In the second
case, something is up with the naming_convention you've passed to
MetaData somewhere, this should be a dictionary value.  If that's not
the case then please confirm the version of SQLAlchemy in use.  0.9 is
very old so you might need to upgrade or at least test that the
SQLAlchemy version resolves.


>
> Question: are online migrations run within a single transaction or not?

this depends on if the database supports transactional DDL (Postgresql
does) and what you have the transaction_per_migration flag set
towards: 
http://alembic.zzzcomputing.com/en/latest/api/runtime.html?highlight=transaction_per_migration#alembic.runtime.environment.EnvironmentContext.configure.params.transaction_per_migration
defaults to False so all migrations are in one transaction for
Postgresql.



>
> Thanks!!!
>
>
> $ alembic upgrade head
>
> /Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-packages/psycopg2/__init__.py:144:
> UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in
> order to keep installing from binary please use "pip install
> psycopg2-binary" instead. For details see:
> .
>
>  """)
>
> INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
>
> INFO [alembic.runtime.migration] Will assume transactional DDL.
>
> INFO [alembic.runtime.migration] Running upgrade 55ebe08ba589 ->
> b6c3cd0ef4cb, version 1
>
> Traceback (most recent call last):
>
>  File "/Users/hp3/.pyenv/versions/server_py2/bin/alembic", line 11, in
> 
>
>  sys.exit(main())
>
>  File
> "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-packages/alembic/config.py",
> line 486, in main
>
>  CommandLine(prog=prog).main(argv=argv)
>
>  File
> "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-packages/alembic/config.py",
> line 480, in main
>
>  self.run_cmd(cfg, options)
>
>  File
> "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-packages/alembic/config.py",
> line 463, in run_cmd
>
>  **dict((k, getattr(options, k, None)) for k in kwarg)
>
>  File
> "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-packages/alembic/command.py",
> line 254, in upgrade
>
>  script.run_env()
>
>  File
> "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-packages/alembic/script/base.py",
> line 427, in run_env
>
>  util.load_python_file(self.dir, 'env.py')
>
>  File
> "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-packages/alembic/util/pyfiles.py",
> line 81, in load_python_file
>
>  module = load_module_py(module_id, path)
>
>  File
> "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-packages/alembic/util/compat.py",
> line 135, in load_module_py
>
>  mod = imp.load_source(module_id, path, fp)
>
>  File "alembic/env.py", line 72, in 
>
>  run_migrations_online()
>
>  File "alembic/env.py", line 67, in run_migrations_online
>
>  context.run_migrations()
>
>  File "", line 8, in run_migrations
>
>  File
> "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-packages/alembic/runtime/environment.py",
> line 836, in run_migrations
>
>  self.get_context().run_migrations(**kw)
>
>  File
> "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-packages/alembic/runtime/migration.py",
> line 330, in run_migrations
>
>  step.migration_fn(**kw)
>
>  File
> "/Users/hp3/Documents/python_workspace/Plannotate2_Cloud/src/server/alembic/versions/b6c3cd0ef4cb_version_1.py",
> line 38, in upgrade
>
>  op.drop_table('groups_history')
>
>  File "", line 8, in drop_table
>
>  File "", line 3, in drop_table
>
>  File
> "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-packages/alembic/operations/ops.py",
> line 1187, in drop_table
>
>  operations.invoke(op)
>
>  File

Re: [sqlalchemy] how to set column default value ?

2018-09-06 Thread Mike Bayer
for ALTER, which is not what you illustrated earlier, server default
comparison is turned off by default as it can be unreliable in some
cases.  Turn it on using compare_server_default=True:
http://alembic.zzzcomputing.com/en/latest/api/runtime.html#alembic.runtime.environment.EnvironmentContext.configure.params.compare_server_default



On Thu, Sep 6, 2018 at 12:37 PM, Yingchen Zhang  wrote:
> like this:
>
> old model:
>
> class TestModel:
>
> ip = db.Column(db.INET, nullable=False)
>
>
> new Model:
>
> class TestModel:
>
> ip = db.Column(db.VARCHAR(), nullable=False, server_default='127.0.0.1')
>
>
> got migration:
>
> op.alter_column('test', 'ip',
> existing_type=postgresql.INET(),
> type_=sa.VARCHAR(),
> nullable=False)
>
>
> no default set.
>
>
> 在 2018年9月6日星期四 UTC+8下午9:54:23,Mike Bayer写道:
>>
>> On Thu, Sep 6, 2018 at 3:50 AM, Yingchen Zhang 
>> wrote:
>> > so, alembic not support `default` ( pgsql create columnn ) ?
>>
>> it does.  use server_default."default" in SQLAlchemy refers to a
>> Python-side default.
>>
>>
>> >
>> > 在 2018年9月6日星期四 UTC+8上午12:24:54,Mike Bayer写道:
>> >>
>> >> On Wed, Sep 5, 2018 at 11:51 AM, Yingchen Zhang 
>> >> wrote:
>> >> > db.Column('text_column', db.VARCHAR(20), default='test_text',
>> >> > server_default='test_text', nullable=True)
>> >> >
>> >> > but, generated code like:
>> >> >
>> >> > sa.Column('text_column', sa.VARCHAR(20), nullable=True),
>> >>
>> >> I assume you are using Alembic autogeneration.   The Python-side
>> >> default "test_text" is not needed in an alembic autogeneration.  The
>> >> server_default however will be rendered.
>> >>
>> >>
>> >> I ran your column as given:
>> >>
>> >> target_metadata = MetaData()
>> >>
>> >> Table(
>> >> 't', target_metadata,
>> >> Column('text_column', VARCHAR(20), default='test_text',
>> >> server_default='test_text', nullable=True)
>> >> )
>> >>
>> >> alembic generates:
>> >>
>> >> def upgrade():
>> >> # ### commands auto generated by Alembic - please adjust! ###
>> >> op.create_table('t',
>> >> sa.Column('text_column', sa.VARCHAR(length=20),
>> >> server_default='test_text', nullable=True)
>> >> )
>> >> # ### end Alembic commands ###
>> >>
>> >>
>> >>
>> >> >
>> >> >
>> >> > how to set column default value ?
>> >> >
>> >> > --
>> >> > 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+...@googlegroups.com.
>> >> > To post to this group, send email to sqlal...@googlegroups.com.
>> >> > Visit this group at https://groups.google.com/group/sqlalchemy.
>> >> > For more options, visit https://groups.google.com/d/optout.
>> >
>> > --
>> > SQLAlchemy -
>> > The Python SQL Toolkit and Object Relational Mapper
>> >
>> > http://www.sqlalchemy.org/
>> >
>> > To post example code, please provide an MCVE: Minimal, Complete, and
>> > Verifiable Example. See http://stackoverflow.com/help/mcve for a full
>> > description.
>> > ---
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "sqlalchemy" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to sqlalchemy+...@googlegroups.com.
>> > To post to this group, send email to sqlal...@googlegroups.com.
>> > Visit this group at https://groups.google.com/group/sqlalchemy.
>> > For more options, visit https://groups.google.com/d/optout.
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

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

online vs offline produce different "python stack traces" and general question

2018-09-06 Thread HP3
Hello,

As I try alembic (for the first time), I ran into the following stack 
traces when attempting to conduct a "nasty" migration.

I call it "nasty" because the original model was based on sqlalchemy v0.9 + 
python 2.7 and the new model is sqlalchemy v1.2 + python 3.6. 
ON TOP OF THAT, it's a massive table and model restructuring. Backend is 
postgres.

I am thinking that I can leverage alembic to do the heavy lifting of 
creating/dropping stuff while I can interject my own bulk_inserts and 
bulk_updates and preserve the existing data.  Something like:

// versions/deadbeef_nasty.py

def upgrade():
op.drop_table(...)
op.drop_table(...)
...

# Here is where I am planning to add my bulk_inserts and bulk_updates

op.add_column(...)
op.drop_column(...)
...



*Is this a reasonable approach?*

*With regards to the stack traces, they differ between online (first stack) 
and offline (second). Hence, this post.*
*Does that make sense?*

*Question: are online migrations run within a single transaction or not?*

*Thanks!!!*


$ alembic upgrade head 

/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package 
will be renamed from release 2.8; in order to keep installing from binary 
please use "pip install psycopg2-binary" instead. For details see: 
. 


 """) 

INFO [alembic.runtime.migration] Context impl PostgresqlImpl. 

INFO [alembic.runtime.migration] Will assume transactional DDL. 

INFO [alembic.runtime.migration] Running upgrade 55ebe08ba589 -> 
b6c3cd0ef4cb, version 1 

Traceback (most recent call last): 

 File "/Users/hp3/.pyenv/versions/server_py2/bin/alembic", line 11, in 
 

 sys.exit(main()) 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/alembic/config.py", line 486, in main 

 CommandLine(prog=prog).main(argv=argv) 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/alembic/config.py", line 480, in main 

 self.run_cmd(cfg, options) 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/alembic/config.py", line 463, in run_cmd 

 **dict((k, getattr(options, k, None)) for k in kwarg) 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/alembic/command.py", line 254, in upgrade 

 script.run_env() 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/alembic/script/base.py", line 427, in run_env 

 util.load_python_file(self.dir, 'env.py') 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/alembic/util/pyfiles.py", line 81, in load_python_file 

 module = load_module_py(module_id, path) 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/alembic/util/compat.py", line 135, in load_module_py 

 mod = imp.load_source(module_id, path, fp) 

 File "alembic/env.py", line 72, in  

 run_migrations_online() 

 File "alembic/env.py", line 67, in run_migrations_online 

 context.run_migrations() 

 File "", line 8, in run_migrations 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/alembic/runtime/environment.py", line 836, in run_migrations 

 self.get_context().run_migrations(**kw) 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/alembic/runtime/migration.py", line 330, in run_migrations 

 step.migration_fn(**kw) 

 File "/Users/hp3/Documents/python_workspace/Plannotate2_Cloud/src/server/
alembic/versions/b6c3cd0ef4cb_version_1.py", line 38, in upgrade 

 op.drop_table('groups_history') 

 File "", line 8, in drop_table 

 File "", line 3, in drop_table 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/alembic/operations/ops.py", line 1187, in drop_table 

 operations.invoke(op) 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/alembic/operations/base.py", line 319, in invoke 

 return fn(self, operation) 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/alembic/operations/toimpl.py", line 70, in drop_table 

 operation.to_table(operations.migration_context) 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/alembic/ddl/impl.py", line 198, in drop_table 

 self._exec(schema.DropTable(table)) 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/alembic/ddl/impl.py", line 115, in _exec 

 return conn.execute(construct, *multiparams, **params) 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-
packages/sqlalchemy/engine/base.py", line 729, in execute 

 return meth(self, multiparams, params) 

 File "/Users/hp3/.pyenv/versions/2.7.14/envs/server_py2/lib/python2.7/site-

Re: [sqlalchemy] how to set column default value ?

2018-09-06 Thread Yingchen Zhang
like this:

old model:

class TestModel:

ip = db.Column(db.INET, nullable=False)


new Model:

class TestModel:

ip = db.Column(db.VARCHAR(), nullable=False, server_default='127.0.0.1')


got migration:

op.alter_column('test', 'ip',
existing_type=postgresql.INET(),
type_=sa.VARCHAR(),
nullable=False)


no default set.


在 2018年9月6日星期四 UTC+8下午9:54:23,Mike Bayer写道:
>
> On Thu, Sep 6, 2018 at 3:50 AM, Yingchen Zhang  > wrote: 
> > so, alembic not support `default` ( pgsql create columnn ) ? 
>
> it does.  use server_default."default" in SQLAlchemy refers to a 
> Python-side default. 
>
>
> > 
> > 在 2018年9月6日星期四 UTC+8上午12:24:54,Mike Bayer写道: 
> >> 
> >> On Wed, Sep 5, 2018 at 11:51 AM, Yingchen Zhang  
> >> wrote: 
> >> > db.Column('text_column', db.VARCHAR(20), default='test_text', 
> >> > server_default='test_text', nullable=True) 
> >> > 
> >> > but, generated code like: 
> >> > 
> >> > sa.Column('text_column', sa.VARCHAR(20), nullable=True), 
> >> 
> >> I assume you are using Alembic autogeneration.   The Python-side 
> >> default "test_text" is not needed in an alembic autogeneration.  The 
> >> server_default however will be rendered. 
> >> 
> >> 
> >> I ran your column as given: 
> >> 
> >> target_metadata = MetaData() 
> >> 
> >> Table( 
> >> 't', target_metadata, 
> >> Column('text_column', VARCHAR(20), default='test_text', 
> >> server_default='test_text', nullable=True) 
> >> ) 
> >> 
> >> alembic generates: 
> >> 
> >> def upgrade(): 
> >> # ### commands auto generated by Alembic - please adjust! ### 
> >> op.create_table('t', 
> >> sa.Column('text_column', sa.VARCHAR(length=20), 
> >> server_default='test_text', nullable=True) 
> >> ) 
> >> # ### end Alembic commands ### 
> >> 
> >> 
> >> 
> >> > 
> >> > 
> >> > how to set column default value ? 
> >> > 
> >> > -- 
> >> > 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+...@googlegroups.com. 
> >> > To post to this group, send email to sqlal...@googlegroups.com. 
> >> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> >> > For more options, visit https://groups.google.com/d/optout. 
> > 
> > -- 
> > SQLAlchemy - 
> > The Python SQL Toolkit and Object Relational Mapper 
> > 
> > http://www.sqlalchemy.org/ 
> > 
> > To post example code, please provide an MCVE: Minimal, Complete, and 
> > Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> > description. 
> > --- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "sqlalchemy" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an 
> > email to sqlalchemy+...@googlegroups.com . 
> > To post to this group, send email to sqlal...@googlegroups.com 
> . 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Re: jsonb_agg issue

2018-09-06 Thread Антонио Антуан
Ok, that works almost fine:
>>> func.jsonb_agg(literal_column(models.OrderUINTable.name + '.*'))
'jsonb_agg(OrderUIN.*)'
The problem is OrderUIN. It must be quoted: jsonb_agg("OrderUIN".*)

чт, 6 сент. 2018 г. в 17:44, Антонио Антуан :

> Another one fail:
> func.jsonb_agg(*models.OrderUINTable.c.values()) - PG error: "function
> jsonb_agg(integer, text, numeric, text, text, text, text) does not exist"
>
>
> четверг, 6 сентября 2018 г., 17:28:37 UTC+3 пользователь Антонио Антуан
> написал:
>
>> Hi guys.
>> I'm using sqlalchemy 1.1.13, want to construct that query:
>> select o.*, jsonb_agg(ou.*) from "Orders" o left join "OrderUIN" ou on
>> ou."OrderID" = o."OrderID" group by o."OrderID"
>>
>> I tried that options:
>> - func.jsonb_agg(OrderUINTable), got select o.*, jsonb_agg() from
>> "Orders"... Empty argument for the function, as you can see.
>> - func.jsonb_agg(OrderUINTable.columns.values()), got can't adapt type
>> 'Column'
>> - func.jsonb_agg(OrderUINTable.alias), got TypeError: self_group() got an
>> unexpected keyword argument 'against'
>>
>> How can I implement it?
>>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>
-- 

Антон

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Re: jsonb_agg issue

2018-09-06 Thread Антонио Антуан
Another one fail:
func.jsonb_agg(*models.OrderUINTable.c.values()) - PG error: "function 
jsonb_agg(integer, text, numeric, text, text, text, text) does not exist"


четверг, 6 сентября 2018 г., 17:28:37 UTC+3 пользователь Антонио Антуан 
написал:
>
> Hi guys.
> I'm using sqlalchemy 1.1.13, want to construct that query:
> select o.*, jsonb_agg(ou.*) from "Orders" o left join "OrderUIN" ou on 
> ou."OrderID" = o."OrderID" group by o."OrderID"
>
> I tried that options:
> - func.jsonb_agg(OrderUINTable), got select o.*, jsonb_agg() from 
> "Orders"... Empty argument for the function, as you can see.
> - func.jsonb_agg(OrderUINTable.columns.values()), got can't adapt type 
> 'Column'
> - func.jsonb_agg(OrderUINTable.alias), got TypeError: self_group() got an 
> unexpected keyword argument 'against'
>
> How can I implement it?
>

-- 
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] jsonb_agg issue

2018-09-06 Thread Антонио Антуан
Hi guys.
I'm using sqlalchemy 1.1.13, want to construct that query:
select o.*, jsonb_agg(ou.*) from "Orders" o left join "OrderUIN" ou on 
ou."OrderID" = o."OrderID" group by o."OrderID"

I tried that options:
- func.jsonb_agg(OrderUINTable), got select o.*, jsonb_agg() from 
"Orders"... Empty argument for the function, as you can see.
- func.jsonb_agg(OrderUINTable.columns.values()), got can't adapt type 
'Column'
- func.jsonb_agg(OrderUINTable.alias), got TypeError: self_group() got an 
unexpected keyword argument 'against'

How can I implement it?

-- 
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] Setting entity related object via related attribute (using init_scalar event)

2018-09-06 Thread Mike Bayer
OK the use case for the init_scalar, and the docs/example do a bad job
of explaining this, is to sync up the default value with that of a
Core-level default generator.That's not clear so I should improve
those docs.

On Thu, Sep 6, 2018 at 10:10 AM, Mike Bayer  wrote:
> On Thu, Sep 6, 2018 at 4:21 AM, Tomáš Sandrini  wrote:
>> Hi,
>>
>> I am writing a library where I need to hide (for normal usage, since that
>> will be 90% use case) the existence of a Parent object,
>> basically limiting it just to one property and then set the Parent manually
>> within the Child class through this one property.
>> An obvious solution to this would be to get the session within the
>> constructor and set it from here, but that breaks the ORM philosophy
>> and I would like to find something more cleaner.
>> So I found the 'init_scalar' ORM event, which is perfectly suited for this
>> issue, but unfortunately it doesn't fire at all for me.
>
>
> the init_scalar event is a strange little event to suit one of the
> less appealing things SQLAlchemy has to do, which is to give you an
> answer when you access an attribute on an object that has no value.
> By "access", we mean reading it, not anything else.  E.g.:
>
> print(Children.parent_id)
>
> The answer SQLAlchemy gives you in modern versions is "None", and
> nothing happens to the state of the object.  In older versions, it
> used to actually set the attribute's state to None, that is, reading
> the attribute had a side effect.When I fixed the attribute system
> to not need that anymore, the events were added in case someone wanted
> to customize this, in that i can restore the old behavior.I doubt
> anyone uses this event for anything, though. I'm not even sure why I
> wrote all those docs for it (and even an example...) because it seems
> kind of strange you'd want to persist a special value only if someone
> "read" from it, I guess I was nervous about people upgrading from the
> old behavior. So I think this is a case of docs being misleading.
>
> In this case, if the thing you need to do your thing is a Session, you
> can catch exactly when that is available using the
> transient_to_pending lifecycle event:
> http://docs.sqlalchemy.org/en/latest/orm/session_events.html?highlight=session%20lifecycle#transient-to-pending
>
> this works
>
> @event.listens_for(Session, "transient_to_pending")
> def init_parent(session, instance):
> if isinstance(instance, Child):   # or whatever mixin, or call a
> method on the class, etc.
> with sess.no_autoflush:
> parent = sess.query(Parent).filter(Parent.name ==
> instance.name).one()
> instance.parent = parent
>
>
>
>
>
>
>
>
>
>>
>> This snippet describes what I am trying to accomplish ->
>> https://pastebin.com/GEccd7zu
>>
>> I will appreciate any kind of help.
>>
>> --
>> SQLAlchemy -
>> The Python SQL Toolkit and Object Relational Mapper
>>
>> http://www.sqlalchemy.org/
>>
>> To post example code, please provide an MCVE: Minimal, Complete, and
>> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
>> description.
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "sqlalchemy" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to sqlalchemy+unsubscr...@googlegroups.com.
>> To post to this group, send email to sqlalchemy@googlegroups.com.
>> Visit this group at https://groups.google.com/group/sqlalchemy.
>> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Setting entity related object via related attribute (using init_scalar event)

2018-09-06 Thread Mike Bayer
On Thu, Sep 6, 2018 at 4:21 AM, Tomáš Sandrini  wrote:
> Hi,
>
> I am writing a library where I need to hide (for normal usage, since that
> will be 90% use case) the existence of a Parent object,
> basically limiting it just to one property and then set the Parent manually
> within the Child class through this one property.
> An obvious solution to this would be to get the session within the
> constructor and set it from here, but that breaks the ORM philosophy
> and I would like to find something more cleaner.
> So I found the 'init_scalar' ORM event, which is perfectly suited for this
> issue, but unfortunately it doesn't fire at all for me.


the init_scalar event is a strange little event to suit one of the
less appealing things SQLAlchemy has to do, which is to give you an
answer when you access an attribute on an object that has no value.
By "access", we mean reading it, not anything else.  E.g.:

print(Children.parent_id)

The answer SQLAlchemy gives you in modern versions is "None", and
nothing happens to the state of the object.  In older versions, it
used to actually set the attribute's state to None, that is, reading
the attribute had a side effect.When I fixed the attribute system
to not need that anymore, the events were added in case someone wanted
to customize this, in that i can restore the old behavior.I doubt
anyone uses this event for anything, though. I'm not even sure why I
wrote all those docs for it (and even an example...) because it seems
kind of strange you'd want to persist a special value only if someone
"read" from it, I guess I was nervous about people upgrading from the
old behavior. So I think this is a case of docs being misleading.

In this case, if the thing you need to do your thing is a Session, you
can catch exactly when that is available using the
transient_to_pending lifecycle event:
http://docs.sqlalchemy.org/en/latest/orm/session_events.html?highlight=session%20lifecycle#transient-to-pending

this works

@event.listens_for(Session, "transient_to_pending")
def init_parent(session, instance):
if isinstance(instance, Child):   # or whatever mixin, or call a
method on the class, etc.
with sess.no_autoflush:
parent = sess.query(Parent).filter(Parent.name ==
instance.name).one()
instance.parent = parent









>
> This snippet describes what I am trying to accomplish ->
> https://pastebin.com/GEccd7zu
>
> I will appreciate any kind of help.
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] how to set column default value ?

2018-09-06 Thread Mike Bayer
On Thu, Sep 6, 2018 at 3:50 AM, Yingchen Zhang  wrote:
> so, alembic not support `default` ( pgsql create columnn ) ?

it does.  use server_default."default" in SQLAlchemy refers to a
Python-side default.


>
> 在 2018年9月6日星期四 UTC+8上午12:24:54,Mike Bayer写道:
>>
>> On Wed, Sep 5, 2018 at 11:51 AM, Yingchen Zhang 
>> wrote:
>> > db.Column('text_column', db.VARCHAR(20), default='test_text',
>> > server_default='test_text', nullable=True)
>> >
>> > but, generated code like:
>> >
>> > sa.Column('text_column', sa.VARCHAR(20), nullable=True),
>>
>> I assume you are using Alembic autogeneration.   The Python-side
>> default "test_text" is not needed in an alembic autogeneration.  The
>> server_default however will be rendered.
>>
>>
>> I ran your column as given:
>>
>> target_metadata = MetaData()
>>
>> Table(
>> 't', target_metadata,
>> Column('text_column', VARCHAR(20), default='test_text',
>> server_default='test_text', nullable=True)
>> )
>>
>> alembic generates:
>>
>> def upgrade():
>> # ### commands auto generated by Alembic - please adjust! ###
>> op.create_table('t',
>> sa.Column('text_column', sa.VARCHAR(length=20),
>> server_default='test_text', nullable=True)
>> )
>> # ### end Alembic commands ###
>>
>>
>>
>> >
>> >
>> > how to set column default value ?
>> >
>> > --
>> > 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+...@googlegroups.com.
>> > To post to this group, send email to sqlal...@googlegroups.com.
>> > Visit this group at https://groups.google.com/group/sqlalchemy.
>> > For more options, visit https://groups.google.com/d/optout.
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
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] Setting entity related object via related attribute (using init_scalar event)

2018-09-06 Thread Tomáš Sandrini
Hi, 

I am writing a library where I need to hide (for normal usage, since that 
will be 90% use case) the existence of a Parent object, 
basically limiting it just to one property and then set the Parent manually 
within the Child class through this one property.
An obvious solution to this would be to get the session within the 
constructor and set it from here, but that breaks the ORM philosophy
and I would like to find something more cleaner.
So I found the 'init_scalar' ORM event, which is perfectly suited for this 
issue, but unfortunately it doesn't fire at all for me.

This snippet describes what I am trying to accomplish -> 
https://pastebin.com/GEccd7zu

I will appreciate any kind of help.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] how to set column default value ?

2018-09-06 Thread Yingchen Zhang
so, alembic not support `default` ( pgsql create columnn ) ?

在 2018年9月6日星期四 UTC+8上午12:24:54,Mike Bayer写道:
>
> On Wed, Sep 5, 2018 at 11:51 AM, Yingchen Zhang  > wrote: 
> > db.Column('text_column', db.VARCHAR(20), default='test_text', 
> > server_default='test_text', nullable=True) 
> > 
> > but, generated code like: 
> > 
> > sa.Column('text_column', sa.VARCHAR(20), nullable=True), 
>
> I assume you are using Alembic autogeneration.   The Python-side 
> default "test_text" is not needed in an alembic autogeneration.  The 
> server_default however will be rendered. 
>
>
> I ran your column as given: 
>
> target_metadata = MetaData() 
>
> Table( 
> 't', target_metadata, 
> Column('text_column', VARCHAR(20), default='test_text', 
> server_default='test_text', nullable=True) 
> ) 
>
> alembic generates: 
>
> def upgrade(): 
> # ### commands auto generated by Alembic - please adjust! ### 
> op.create_table('t', 
> sa.Column('text_column', sa.VARCHAR(length=20), 
> server_default='test_text', nullable=True) 
> ) 
> # ### end Alembic commands ### 
>
>
>
> > 
> > 
> > how to set column default value ? 
> > 
> > -- 
> > 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+...@googlegroups.com . 
> > To post to this group, send email to sqlal...@googlegroups.com 
> . 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.