HelloI'm trying to understand compilation and working through the compiler tutorial: https://docs.sqlalchemy.org/en/14/core/compiler.htmlI was hoping for some clarification :1) What is the difference between starting your method with visit_XXX instead of compile_XX?
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.
3) What is the purpose of the ... in the visit_alter_column method?
4) What is the example usage for AlterColumn? I tried this:from sqlalchemy import create_enginefrom sqlalchemy import Table, Column, Integer, String, MetaDataengine = 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.
5) How would I create an AddColumn function? Something like this perhaps?class AddColumn(DDLElement):def __init__(self, column, column_type):self.column = columnself.column_type = column_type@compiles(AddColumn)def visit_add_column(element, compiler, **kw):return "ALTER TABLE %s ADD COLUMN %s ..." % (element.table.name,
RegardsSoumaya
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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 view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/0b3e9438-7631-46d8-bd3a-8f835a8c11c1n%40googlegroups.com.
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.schema import DDLElement
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData
engine = create_engine('sqlite:///:memory:', echo=True)
metadata = MetaData()
users = Table('users', metadata,
Column('id', Integer),
)
metadata.create_all(engine)
class AddColumn(DDLElement):
def __init__(self, column_name, column_type):
self.column_name = column_name
self.column_type = column_type
@compiles(AddColumn)
def visit_add_column(element, column, compiler, **kw):
return "ALTER TABLE %s ADD COLUMN %s %s %s" % (element.table.name, AddColumn.column_name, AddColumn.column_type)
s = AddColumn(users,'col1', 'int')
print(str(s))
with engine.connect() as conn:
conn.execute(AddColumn(users,'col1', 'int'))
print(conn.execute(users.select()).fetchall())
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/e1053691-8d35-4452-9826-c4f4df04dbda%40www.fastmail.com.
Hello MikeThanks for the quick response. Would you mind helping me write a complete example? It would help me understand compilation better :-)
I would also be happy to turn this into a merge request if you think other people will benefit from it?
I have also converted this into a sqlite example so using add column instead of alter column, to make it more generic and follow the sql expression tutorial. Also in the example I pass in the column type as a string, how would I pass in the type as a sqlalchemy data type?
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAN14jWTbx5tKqfTjkyuL-3KLY6Gyykzrq8Qyvur-AqDMUqFWfw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/3f37f7c7-f13f-420d-b18d-b7cba086cad6%40www.fastmail.com.
Hello Mike
Your example was really helpful thanks! I hadn't realised you should access the variables defined in AlterColumn through the element variable, so my working example I posted before failed but but now works.If you wanted to expand the synopsis and make it more newbie-friendly I would include the following:-add a link to explanation of compilation (in the first line)
-explanation of callable (as opposed to method)
-how the subclass needs to inherit from one of the classes defined in the "subclassing guidelines"
ClauseElement
subclasses and one or
more callables defining its compilation:"-how the function that defines the compilation could be called anything as long as has the @compiles decorator, but recommendation is to start with compile_ or visit_
-explanation of each parameter in compile_mycolumn and what is their purpose
-how compile_mycolumn inherits the name attribute from ColumnClause so you don't need to specify additional attributes in MyColumn class
-if you did need to specify additional attributes, you should specify them in the class construct, as in the AlterColumn example
-replace print(str(s)) with print(s) (unless there's some difference I'm not aware off)
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAN14jWSd2%2Bd0CtCjjE1QCJD4J6A3eSxGs-AcM2Vn%3DmXGfZHLgQ%40mail.gmail.com.