Re: [sqlalchemy] Re: Connection error

2021-06-16 Thread Rich Shepard

On Wed, 16 Jun 2021, Ryan Bandler wrote:


Yeah I connect to the DB over localhost:5432 via psql all the time and
also haven't had any issues connecting via psycopg2. I also have no issue
connecting to the database in sqlalchem, it's only an issue with
sqlacodegen.


Ryan,

Just to confirm, you're running sqlacodegen from the OS shell and not from
the python REPL, correct?

Rich

--
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/alpine.LNX.2.20.2106160808450.6174%40salmo.appl-ecosys.com.


[sqlalchemy] Re: Connection error

2021-06-16 Thread Ryan Bandler
Anyways guys I was able to fix the issue by installing a flask-specific 
version of the same package (even though I am not using flask) and I was 
able to get it to work just fine. Thanks everyone for your help.

On Wednesday, June 16, 2021 at 9:20:37 AM UTC-4 Ryan Bandler wrote:

> Yeah I connect to the DB over localhost:5432 via psql all the time and 
> also haven't had any issues connecting via psycopg2. I also have no issue 
> connecting to the database in sqlalchem, it's only an issue with 
> sqlacodegen.
> On Tuesday, June 15, 2021 at 7:53:51 PM UTC-4 jonatha...@gmail.com wrote:
>
>> Have you confirmed that you can connect to 127.0.0.1 at port 5432 using 
>> psql? On my development system, I normally use a local (UNIX domain) 
>> socket, which is libpq's default behavior. When I run "psql -h 127.0.0.1", 
>> I get the following error:
>>
>> psql: could not connect to server: Connection refused
>> Is the server running on host "127.0.0.1" and accepting
>>
>> TCP/IP connections on port 5432?
>>
>> On Tuesday, June 15, 2021 at 1:06:23 PM UTC-4 Ryan Bandler wrote:
>>
>>> Hello everyone,
>>>
>>> I am a first-time SQLalchemy user planning on using SQLalchemy on a new 
>>> project. We already have an established postgres database (currently still 
>>> on localhost) which I do not want to handwrite the SQLalchemy model for. So 
>>> I am planning to use sqlacodegen. Unfortunately, sqlacodegen is giving me:
>>>
>>> sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not 
>>> connect to server: Connection refused
>>> Is the server running on host "localhost" (127.0.0.1) and 
>>> accepting
>>> TCP/IP connections on port 5432?
>>>
>>> My systems at work are very restrictive, but I was able to gain 
>>> permissions to the postgres config files and edited them to allow all TCP 
>>> connections. I restarted the postgres service and I am still experiencing 
>>> this error. I dont understand why this is happening, because it seems to be 
>>> an error coming from psycopg2 being called in sqlalchemy, and i have ever 
>>> had any issues connecting to the DB with psycopg2 before. 
>>>
>>> If anyone has any experience with sqlacodegen, any help would be much 
>>> appreciated!!
>>>
>>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/ef5f8cda-983b-4657-9020-ad6be4201522n%40googlegroups.com.


[sqlalchemy] Re: Connection error

2021-06-16 Thread Ryan Bandler
Yeah I connect to the DB over localhost:5432 via psql all the time and also 
haven't had any issues connecting via psycopg2. I also have no issue 
connecting to the database in sqlalchem, it's only an issue with 
sqlacodegen.
On Tuesday, June 15, 2021 at 7:53:51 PM UTC-4 jonatha...@gmail.com wrote:

> Have you confirmed that you can connect to 127.0.0.1 at port 5432 using 
> psql? On my development system, I normally use a local (UNIX domain) 
> socket, which is libpq's default behavior. When I run "psql -h 127.0.0.1", 
> I get the following error:
>
> psql: could not connect to server: Connection refused
> Is the server running on host "127.0.0.1" and accepting
>
> TCP/IP connections on port 5432?
>
> On Tuesday, June 15, 2021 at 1:06:23 PM UTC-4 Ryan Bandler wrote:
>
>> Hello everyone,
>>
>> I am a first-time SQLalchemy user planning on using SQLalchemy on a new 
>> project. We already have an established postgres database (currently still 
>> on localhost) which I do not want to handwrite the SQLalchemy model for. So 
>> I am planning to use sqlacodegen. Unfortunately, sqlacodegen is giving me:
>>
>> sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not 
>> connect to server: Connection refused
>> Is the server running on host "localhost" (127.0.0.1) and 
>> accepting
>> TCP/IP connections on port 5432?
>>
>> My systems at work are very restrictive, but I was able to gain 
>> permissions to the postgres config files and edited them to allow all TCP 
>> connections. I restarted the postgres service and I am still experiencing 
>> this error. I dont understand why this is happening, because it seems to be 
>> an error coming from psycopg2 being called in sqlalchemy, and i have ever 
>> had any issues connecting to the DB with psycopg2 before. 
>>
>> If anyone has any experience with sqlacodegen, any help would be much 
>> appreciated!!
>>
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/5a5cd4de-df65-4713-97e5-317910dddf79n%40googlegroups.com.


Re: [sqlalchemy] versioned_history example uses deprecated Column.copy() method

2021-06-16 Thread Mike Bayer
HI Simon -

I believe that example for now should vendor its own "copy()" function that 
does what's needed.   the function that's there is already un-doing some of the 
work of the old copy() method in any case.  I think for history table we need 
column name, datatype, and maybe nullable constraint.

We can accept PRs for this if this is something you would be interested in, 
although your contributions to the SQLA mailing list are already many :)



On Wed, Jun 16, 2021, at 7:15 AM, Simon King wrote:
> Hi all,
> 
> I'm updating an app from SA 1.3 to 1.4 and getting a SADeprecationWarning:
> 
> The Column.copy() method is deprecated and will be removed in a
> future release. (deprecated since: 1.4)
> 
> The code triggering the warning is based on the versioned_history example:
> 
> https://docs.sqlalchemy.org/en/14/_modules/examples/versioned_history/history_meta.html
> 
> ...and here's the offending function:
> 
> def _col_copy(col):
> orig = col
> col = col.copy()
> orig.info["history_copy"] = col
> col.unique = False
> col.default = col.server_default = None
> col.autoincrement = False
> return col
> 
> For the moment I've switched to calling the private _copy() method
> instead, but is there any recommendation of a better approach?
> 
> (I did see the issue at
> https://github.com/sqlalchemy/sqlalchemy/issues/5953 and understand
> why the copy() method was deprecated)
> 
> Thanks a lot,
> 
> Simon
> 
> -- 
> 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 view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/CAFHwexdUVb11FTq%3DzxuzNsp-FszVHrJ8KT-yDjhyNkWqhKoJnQ%40mail.gmail.com.
> 

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/46661334-a2b9-4548-9003-b487b725615d%40www.fastmail.com.


[sqlalchemy] versioned_history example uses deprecated Column.copy() method

2021-06-16 Thread Simon King
Hi all,

I'm updating an app from SA 1.3 to 1.4 and getting a SADeprecationWarning:

The Column.copy() method is deprecated and will be removed in a
future release. (deprecated since: 1.4)

The code triggering the warning is based on the versioned_history example:

https://docs.sqlalchemy.org/en/14/_modules/examples/versioned_history/history_meta.html

...and here's the offending function:

def _col_copy(col):
orig = col
col = col.copy()
orig.info["history_copy"] = col
col.unique = False
col.default = col.server_default = None
col.autoincrement = False
return col

For the moment I've switched to calling the private _copy() method
instead, but is there any recommendation of a better approach?

(I did see the issue at
https://github.com/sqlalchemy/sqlalchemy/issues/5953 and understand
why the copy() method was deprecated)

Thanks a lot,

Simon

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/CAFHwexdUVb11FTq%3DzxuzNsp-FszVHrJ8KT-yDjhyNkWqhKoJnQ%40mail.gmail.com.


Re: [sqlalchemy] issue with hybrid properties

2021-06-16 Thread Julien Cigar
On Mon, Jun 14, 2021 at 10:04:10AM -0400, Mike Bayer wrote:
> Buried deep in this example you are using the "relationship to aliased class" 
> pattern, which is fine, not sure if that was in the previous example, but in 
> this case this is the source of the issue.
> 
> The approach to solving all hybrid related querying issues, as well as assoc 
> proxy issues, is to fully write out the query not using either approach.  
> I see the docs at 
> https://docs.sqlalchemy.org/en/14/orm/join_conditions.html#relationship-to-aliased-class
>  don't clarify that when one makes a relationship to an aliased class, that's 
> your entity now, you can not query this relationship in terms of the original 
> class.   
> 
> so to illustrate test six not using hybrid:
> 
> def test_six_not_using_hybrids(self):
> s = Session(e)
> 
> # the "relationship to aliased class" pattern is in use, and this
> # aliased object was private to the setup_relationships() function,
> # so pull it out here so we can use it in a query
> partition_alias = Document.document_current_translation.entity.entity
> 
> q = (
> s.query(Document)
> .join(Document.document_current_translation)
> .options(orm.lazyload("*"))
> .filter(partition_alias.title.like("doc3%"))
> .order_by(partition_alias.title)
> )

one more question: I'm wondering why does it work without a
.contains_eager(Document.document_current_translation) ?

I thought that it was somewhat mandatory when filtering/ordering on a
relationship which was explicitely joined in the Query (as mentionned in
the "Routing Explicit Joins/Statements into Eagerly Loaded Collections"
documentation)

> 
> 
> Then to get this to work with hybrids, the hybrid also must be set up to use 
> this alias object in queries, not the actual class, as that's not what the 
> relationship is mapped towards:
> 
> def setup_hybrids(
> cls, name, translation_cls, current_locale=get_current_locale, 
> default=None
> ):
> 
> # the "relationship to aliased class" pattern is in use, and this
> # aliased object was private to the setup_relationships() function,
> # so pull it out here so we can use it in a query
> partition_alias = cls.current_translation.entity.entity
> 
> def _fget(self):
> return getattr(self.current_translation, name, default)
> 
> def _fset(self, value):
> locale_name = current_locale()
> 
> trans = self.translations.setdefault(
> locale_name, translation_cls(language_id=locale_name)
> )
> 
> setattr(trans, name, value)
> 
> def _expr(_cls):
> return getattr(partition_alias, name)
> # foo = orm.aliased(cls.document_current_translation)
> # q = sql.select([getattr(foo, name)]).as_scalar()
> # return q
> 
> log.info("Adding hybrid attribute: %s.%s", cls, name)
> 
> prop = hybrid_property(fget=_fget, fset=_fset, expr=_expr)
> 
> setattr(cls, name, prop)
> 
> 
> then your original test_six works as given.
> 
> 
> 
> 
> On Mon, Jun 14, 2021, at 6:20 AM, Julien Cigar wrote:
> > On Tue, Jun 01, 2021 at 08:36:38AM -0400, Mike Bayer wrote:
> > > I can't make out your "ideal" SQL due to bad formatting however, if you 
> > > want the query to have an additional JOIN tacked onto it whenever you 
> > > refer to this hybrid property in the COLUMNS clause, you need to use 
> > > query.join() explicitly, either up front or by using something like a 
> > > query builder function or the do_orm_execute event to automatically alter 
> > > the query.
> > 
> > Thank you Mike, I'm still wondering why with when I'm joining the 
> > row-limited relationship (through a window function) called 
> > "current_translation" in my case the generated FROM is wrong. It looks
> > like there is an issue when the aliased class is joined through
> > inheritance.
> > 
> > Attached my updated POC, the issue is in test_six and test_seven
> > 
> > (pytest -s poc.py)
> > 
> > you can clearly see that the FROM clause is wrong
> > (https://gist.github.com/silenius/3c090ee9470a4889e9812f7d8da64f56#file-gistfile1-txt-L28-L31),
> > although the .current_translation relationship is properly loaded
> > (https://gist.github.com/silenius/3c090ee9470a4889e9812f7d8da64f56#file-gistfile1-txt-L28-L31)
> > 
> > (and I'm sorry, but I'm still don't see how could I subselect from this
> > row-limited current_translation relationship in my hybrid property
> > expression ..)
> > 
> > Have a good day,
> > Julien
> > 
> > > 
> > > On Tue, Jun 1, 2021, at 6:58 AM, Julien Cigar wrote:
> > > > On Fri, May 28, 2021 at 10:53:26AM -0400, Mike Bayer wrote:
> > > > > the hybrid here is pulling in additional tables which are causing a 
> > > > > cartesian product since there is nothing joining the Document entity 
> > > > > to this other entity which seems to be DocumentTranslation.Run 
> > > > > this in 1.4 and you will