On Sun, Jan 24, 2021, at 9:42 AM, sector119 wrote:
> Hello
> 
> Is it possible to set default tablespace for all indexes?
> 
> I know that I cat set it with
> Index('my_index', my_table.c.data, postgresql_tablespace='my_tablespace')
> 

> But I want to set set it by default somehow, that when I just put 
> "index=True" on column, I get index created at some tablespace.

Hey there -

we have the usual slate of answers, I know you've used SQLAlchemy for a long 
time so these should be familiar:

1. make your own function (easiest)

     def tablespace_index(*arg, **kw):
        kw["postgresql_tablespace"] = "my_tablespace"
        return Index(*arg, **kw)

2. intercept DDL at the Python construction level (probably the smoothest 
approach, you can look at "parent" to check the Table)

   @event.listens_for(Index, "before_parent_attach")
   def before_parent_attach(target, parent):
       target.dialect_kwargs["postgresql_tablespace"] = "my_tablespace"

3. intercept DDL at the SQL execution level (heavy handed)

   @event.listens_for(connection, "before_execute")
   def before_execute(elem, clauseelement, multiparams, params, 
execution_options):
       if isinstance(clauseelement, CreateIndex):
            clauseelement.element.dialect_kwargs["postgresql_tablespace"] = 
"my_tablespace"

that's what I got for this one.



> 
> Thank You
> 

> -- 
> SQLAlchemy - 
> The Python SQL Toolkit and Object Relational Mapper
>  
> http://www.sqlalchemy.org/
>  
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> --- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/2d9fca7c-6df1-4632-a97f-43a60cdae4ecn%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/2d9fca7c-6df1-4632-a97f-43a60cdae4ecn%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
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/50133b1d-16df-4098-8c11-a271b1c3bec2%40www.fastmail.com.

Reply via email to