On Mon, Apr 19, 2021, at 2:09 PM, sumau wrote:
> Hello
> 
> I'm  trying to understand compilation and working through the compiler 
> tutorial: https://docs.sqlalchemy.org/en/14/core/compiler.html
> 
> I was hoping for some clarification :
> 
> 1) What is the difference between starting your method with visit_XXX instead 
> of compile_XX?

no difference.  the compiler extension does not rely upon naming conventions, 
just that the decorator is there.  you can name any function anything or even 
use a lambda.

> 
> 2) self.cmd is used in the init of AlterColumn but I can't see where it is 
> used in the visit_alter_column method.

the example is incomplete and "cmd" would represent some kind of alteration to 
the column, in the SQL where you see the ellipses...this is not a fully real 
world example.

> 
> 3) What is the purpose of the ... in the visit_alter_column method? 

in the example there it's a function.   Assuming you mean the function, that's 
where you implement how to turn your AlterColumn object into a SQL string.


> 
> 4) What is the example usage for AlterColumn? I tried this:
> 
> from sqlalchemy import create_engine
> from sqlalchemy import Table, Column, Integer, String, MetaData
> 
> engine = create_engine('postgresql://***:***@localhost:5432/postgres')
> conn = engine.connect()
> 
> metadata = MetaData()
> users = Table('users', metadata,
> Column('id', Integer),
> Column('name', String),
> Column('fullname', String),
> )
> 
> metadata.create_all(engine)
> users.AlterColumn(users.c.name, 'type int')
> 
> without success.

noting that this AlterColumn isn't "real" and the SQL it genreates isn't valid, 
you would execute it:

with engine.connect() as conn:
    conn.execute(AlterColumn(...))


> 
> 5) How would I create an AddColumn function? Something like this perhaps? 
> 
> class AddColumn(DDLElement):
> def __init__(self, column, column_type):
> self.column = column
> self.column_type = column_type
> 
> @compiles(AddColumn)
> def visit_add_column(element, compiler, **kw):
> return "ALTER TABLE %s ADD COLUMN %s ..." % (element.table.name,
> element.column.name)

AddColumn is a class but other than that, that's the idea sure!




> 
> Regards
> Soumaya
> 

> -- 
> 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/0b3e9438-7631-46d8-bd3a-8f835a8c11c1n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/0b3e9438-7631-46d8-bd3a-8f835a8c11c1n%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/e1053691-8d35-4452-9826-c4f4df04dbda%40www.fastmail.com.

Reply via email to