Thanks. I'm gonna read the communication to watch the difference between Core and ORM. I changed that process_files description to Process_Files(Base): #mapping fields
It worked! 2013/3/7 Simon King <[email protected]> > SQLAlchemy is broadly separated into 2 parts, "core" and "ORM" (which you > can see as the left and right-hand sides on the front page of the > documentation, http://docs.sqlalchemy.org/en/rel_0_8/) > > Table objects like the one you have below are part of the Core API. > Columns of Table objects are accessible via the "c" property: > > process_files.c.process_id > > The ORM is built on top of the core API. When you do something like: > > class Files(Base): > __tablename__ = 'files' > id = sa.Column('id', sa.Integer, primary_key=True) > > …you are creating a "mapped class". You can access the columns of these > directly like "Files.id". If you ever need access to the underlying Table > object, you can get it from Files.__table__. > > When using things like session.query(), you can often use tables or mapped > classes interchangeably. > > Hope that helps, > > Simon > > On 7 Mar 2013, at 20:57, Mauricio de Abreu Antunes < > [email protected]> wrote: > > > Simon, > > > > Here is the table (not sure if the code to create a relationship table > is correct): > > > > > > # Many to Many - Process x Files > > > > > > > > process_files = Table("process_files", Base.metadata, > > > > > > > > Column("process_id", Integer, ForeignKey("process.id")), > > > > > > > > Column("files_id", Integer, ForeignKey("files.id")) > > > > > > > > ) > > > > > > > > > > The error: > > > > AttributeError: 'Table' object has no attribute 'process_id' > > > > > > Why can not I access process_id? > > > > 2013/3/7 Simon King <[email protected]> > > The ORM tutorial covers querying with joins: > > > > > http://docs.sqlalchemy.org/en/rel_0_8/orm/tutorial.html#querying-with-joins > > > > In this case, you probably want something like this (if ProcessFiles is > a class mapped to the process_files table): > > > > result = > session.query(Files).join(ProcessFiles).filter(ProcessFiles.process_id==1) > > > > Hope that helps, > > > > Simon > > > > On 7 Mar 2013, at 20:35, Mauricio de Abreu Antunes < > [email protected]> wrote: > > > > > I wanna perform a query on process_files and hereafter a update/join > like this: > > > > > > SELECT files.id AS files_id, files.name AS files_name, > files.directory AS files_directory, files.active AS files_active, > files.connection_id AS files_connection_id > > > FROM files JOIN process_files ON files.id = process_files.files_id > > > > > > Actually the piece os select above is a result from: > > > > > > result = session.query(Files).join(process_files) > > > > > > It is ok, but i wanna filter it with process_files.process_id = 1 for > example. > > > > > > Reading the docs i could not find the proper way to do this: > > > > http://docs.sqlalchemy.org/en/rel_0_7/orm/query.html#sqlalchemy.orm.query.Query.join > > > > > > Am i reading the wrong part of the docs? > > > > > > 2013/3/7 Mauricio de Abreu Antunes <[email protected]> > > > These tips are veeery good! > > > I sometimes get lost about the best way to use the best ORM library in > the world. > > > > > > > > > 2013/3/7 Simon King <[email protected]> > > > This is an unusual way to update an object that you've already > retrieved: > > > > > > result = session.query(Executions). \ > > > filter_by(id=execution_id).first() > > > if result.end_date is None: > > > e = > > > update(Executions).where(Executions.id==bindparam("execution_id")). \ > > > values(end_date=bindparam("now")) > > > self.connection.execute(e, execution_id=execution_id, > > > now=datetime.datetime.now()) > > > > > > It would be more natural to write it like this: > > > > > > if result.end_date is None: > > > result.end_date = datetime.datetime.now() > > > session.flush() > > > > > > Also, if "id" is the primary key on your Executions class, you can > > > write the first line as: > > > > > > result = session.query(Executions).get(execution_id) > > > > > > > > > On Thu, Mar 7, 2013 at 1:58 AM, Michael Bayer < > [email protected]> wrote: > > > > in the 0.7 series, you can't pass an ORM mapped class as the subject > of the > > > > core update() construct: > > > > > > > > e = > update(Executions).where(Executions.id==bindparam("execution_id")). \ > > > > values(end_date=bindparam("now")) > > > > > > > > that statement will work as is if you just refer to the Table: > > > > > > > > e = > > > > > update(Executions.__table__).where(Executions.id==bindparam("execution_id")). > > > > \ > > > > values(end_date=bindparam("now")) > > > > > > > > also note that the indirection between bindparam("foo") and > > > > connection.execute(stmt, foo="some value") is not needed; you can > embed > > > > literal values directly in the statement, and the Core will convert > them to > > > > bound parameters (just use echo=True to see it in action): > > > > > > > > e = update(Executions).where(Executions.id==execution_id). \ > > > > values(end_date=datetime.datetime.now()) > > > > > > > > At the ORM level, you can use query.update(): > > > > > > > > > session.query(Executions).filter(Executions.id==execution_id).update({"end_date":datetime.now()}, > > > > synchronize_session=False) > > > > > > > > > > > > On Mar 6, 2013, at 8:06 PM, Mauricio de Abreu Antunes > > > > <[email protected]> wrote: > > > > > > > > So, i have read @StackOverflow some tips. > > > > There is a lot of people saying they have to make a query on the > table and > > > > then update it. there is no way to upgrade without performing a > query?! > > > > > > > > On Wednesday, March 6, 2013 6:17:35 PM UTC-3, Mauricio de Abreu > Antunes > > > > wrote: > > > >> > > > >> Hello, > > > >> > > > >> I'm new to SQLAlchemy. Currently I'm using SQLAlchemy 0.7.1. > > > >> Reading the tutorial, I tried to write my codes like those examples > but I > > > >> had no success working on it. > > > >> > > > >> Code is here: > > > >> https://gist.github.com/mauricioabreu/5103163 > > > >> > > > >> Do I need to map the table Executions to execute an update > expression on > > > >> it? > > > >> > > > >> Sorry if this is a very noob question. > > > >> > > > >> If you need more info about the problem let me know. > > > > > > > > > > > > -- > > > > 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 [email protected]. > > > > To post to this group, send email to [email protected]. > > > > Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. > > > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > > > > > > > > > > > > > > > > -- > > > > 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 [email protected]. > > > > To post to this group, send email to [email protected]. > > > > Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. > > > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > > > > > > > > > > -- > > > 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 [email protected]. > > > To post to this group, send email to [email protected]. > > > Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. > > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > > > > > > > > > > > > > > -- > > > Mauricio de Abreu Antunes > > > Mobile: (51)930-74-525 > > > Skype: mauricio.abreua > > > > > > > > > > > > -- > > > Mauricio de Abreu Antunes > > > Mobile: (51)930-74-525 > > > Skype: mauricio.abreua > > > > > > -- > > > 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 [email protected]. > > > To post to this group, send email to [email protected]. > > > Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. > > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > > > > > > -- > > 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 [email protected]. > > To post to this group, send email to [email protected]. > > Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > > > > > > > > -- > > Mauricio de Abreu Antunes > > Mobile: (51)930-74-525 > > Skype: mauricio.abreua > > > > -- > > 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 [email protected]. > > To post to this group, send email to [email protected]. > > Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- *Mauricio de Abreu Antunes* Mobile: (51)930-74-525 Skype: mauricio.abreua -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
