Re: [sqlalchemy] Dialect-specific dispatch in user code

2021-10-19 Thread Mike Bayer


On Tue, Oct 19, 2021, at 1:41 PM, Jonathan Brandmeyer wrote:
> We're supporting both Postgres and SQLite in our application.  For the most 
> part, sticking close to ANSI has made this pretty seamless.  However, there 
> have been occasions where we want to write either DDL or DML in a 
> dialect-specific way.
> 
> For column data, we want to enable the use of JSON.  However, when on 
> Postgres we'd like to use JSONB, but when on SQLite we'd use their JSON1 
> extension.  The generic JSON type provided by sqlalchemy defaults to the 
> postgres JSON type when on postgres.  How can we get it to default to JSONB 
> instead?  Using dialect-specific column types in the mapper is a non-starter, 
> because a mapped class (or Core table) may have to work with both a Postgres 
> connection and an SQLite connection in the same program.  We almost want to 
> follow[1], except that I'm concerned that some of the query syntax renderer 
> might also be affected by switching to JSONB.

use with_variant:

Column("data", JSON().with_variant(postgresql.JSONB(), "postgresql"))

https://docs.sqlalchemy.org/en/14/core/type_api.html?highlight=with_variant#sqlalchemy.types.TypeEngine.with_variant



> For DML there are a few cases where we'd like to use the 
> on_conflict_do_nothing syntax.  However, it isn't available as generic 
> syntax, only dialect-specific syntax.  It's not clear how query-generating 
> code can figure out which syntax to use given only a connection. 

this was just asked yesterday and basically you can dispatch on 
connection.engine.name for different database backends.   Simple + more 
generalized decorator approach illustrated at 
https://github.com/sqlalchemy/sqlalchemy/discussions/7199#discussioncomment-1495790


> 
> SQA has some support for adding text() to larger queries, but this is a 
> modifier that doesn't clearly fit with the other generative methods that 
> accept text arguments.  Is there a way to hack on some extra text into the 
> _post_values_clause that will be supported into the future?

not sure what you're looking to do here.



> 
> 
> Sincerely,
> -- 
> Jonathan Brandmeyer
> PlanetiQ
> 
> 
> -- 
> 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/CA%2BXzfko00UCaUn%2B8-KAfWWsqjbj6BG_kLCy0vDa-Sn%2BOteZa4g%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/de4863ac-73d3-4d1b-8f67-e070e10e62fb%40www.fastmail.com.


[sqlalchemy] Dialect-specific dispatch in user code

2021-10-19 Thread Jonathan Brandmeyer
We're supporting both Postgres and SQLite in our application.  For the most
part, sticking close to ANSI has made this pretty seamless.  However, there
have been occasions where we want to write either DDL or DML in a
dialect-specific way.

For column data, we want to enable the use of JSON.  However, when on
Postgres we'd like to use JSONB, but when on SQLite we'd use their JSON1
extension.  The generic JSON type provided by sqlalchemy defaults to the
postgres JSON type when on postgres.  How can we get it to default to JSONB
instead?  Using dialect-specific column types in the mapper is a
non-starter, because a mapped class (or Core table) may have to work with
both a Postgres connection and an SQLite connection in the same program.
We almost want to follow[1], except that I'm concerned that some of the
query syntax renderer might also be affected by switching to JSONB.

[1]:
https://docs.sqlalchemy.org/en/14/core/compiler.html#changing-compilation-of-types

For DML there are a few cases where we'd like to use the
on_conflict_do_nothing syntax.  However, it isn't available as generic
syntax, only dialect-specific syntax.  It's not clear how query-generating
code can figure out which syntax to use given only a connection.  SQA has
some support for adding text() to larger queries, but this is a modifier
that doesn't clearly fit with the other generative methods that accept text
arguments.  Is there a way to hack on some extra text into the
_post_values_clause that will be supported into the future?

Sincerely,
-- 
Jonathan Brandmeyer
PlanetiQ

-- 
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/CA%2BXzfko00UCaUn%2B8-KAfWWsqjbj6BG_kLCy0vDa-Sn%2BOteZa4g%40mail.gmail.com.


Re: [sqlalchemy] Re: SqlAlchemy with Postgres: How can I make a query that checks if a string is in a list inside a json column

2021-10-19 Thread Simon King
For what it's worth, I think the "?" operator would work for this with
JSONB, but not with JSON:

postgres=# select '["user1", "user2"]'::jsonb ? 'user1';
?column?
--
 t
(1 row)

postgres=# select '["user1", "user2"]'::jsonb ? 'user2';
?column?
--
 t
(1 row)

postgres=# select '["user1", "user2"]'::jsonb ? 'user3';
?column?
--
 f
(1 row)


SQLAlchemy surfaces the "?" operator as .has_key

https://docs.sqlalchemy.org/en/14/dialects/postgresql.html#sqlalchemy.dialects.postgresql.JSONB.Comparator.has_key

https://github.com/sqlalchemy/sqlalchemy/blob/main/lib/sqlalchemy/dialects/postgresql/json.py#L33

Simon


On Mon, Oct 18, 2021 at 10:35 PM Jonathan Vanasco  wrote:
>
> I'm not sure, but AFAIK, this type of search isn't *easily* doable in 
> PostgreSQL. The json and jsonb operators and functions are really targeting 
> "object literals" style data, not lists.
>
> https://www.postgresql.org/docs/current/functions-json.html
>
> In the past, I think one could search against the column like text and 
> match/regex out a list value like `"user1"` - but that didn't work right.
>
> This type of search is possible with advanced PostgreSQL queries, by using 
> the functions like json_array_elements on a field and joining against that. 
> That's really not within the scope of SQLAlchemy or this list though, and 
> you'll have better luck search (or asking) on Stack Overflow.  There are a 
> handful of questions and solutions there on this topic.
>
> Once you can figure out the PostgreSQL queries to accomplish what you want, 
> this list can help you convert it to SQLAlchemy if you have trouble.
>
> On Wednesday, October 13, 2021 at 9:50:16 AM UTC-4 chat...@gmail.com wrote:
>>
>> Imagine a Postgres JSON column with values like below:
>>
>> "["user1", "user2"]"
>>
>> Is there any way to query a postgres JSON (not JSONB) column with 
>> SqlAlchemy,like above that checks if the value "user1" is contained in this 
>> column?
>
> --
> 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/8f1986f6-4a39-4cad-93f2-a8d1c392b4b2n%40googlegroups.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/CAFHwexfnFazzHYEgM%2BKt9QqcHM-HQFWkpjofvih%3Dm5GNL5G4dQ%40mail.gmail.com.