• Complain

it-ebooks - SQLAlchemy 1.1 Documentation

Here you can read online it-ebooks - SQLAlchemy 1.1 Documentation full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2017, publisher: iBooker it-ebooks, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

No cover
  • Book:
    SQLAlchemy 1.1 Documentation
  • Author:
  • Publisher:
    iBooker it-ebooks
  • Genre:
  • Year:
    2017
  • Rating:
    5 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 100
    • 1
    • 2
    • 3
    • 4
    • 5

SQLAlchemy 1.1 Documentation: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "SQLAlchemy 1.1 Documentation" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

it-ebooks: author's other books


Who wrote SQLAlchemy 1.1 Documentation? Find out the surname, the name of the author of the book and a list of all author's works by series.

SQLAlchemy 1.1 Documentation — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "SQLAlchemy 1.1 Documentation" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Additional Persistence Techniques
Embedding SQL Insert/Update Expressions into a Flush

This feature allows the value of a database column to be set to a SQLexpression instead of a literal value. Its especially useful for atomicupdates, calling stored procedures, etc. All you do is assign an expression toan attribute:

class SomeClass ( object ): pass mapper ( SomeClass , some_table ) someobject = session . query ( SomeClass ) . get ( ) # set 'value' attribute to a SQL expression adding one someobject . value = some_table . c . value + # issues "UPDATE some_table SET value=value+1" session . commit ()

This technique works both for INSERT and UPDATE statements. After theflush/commit operation, the value attribute on someobject above isexpired, so that when next accessed the newly generated value will be loadedfrom the database.

Using SQL Expressions with Sessions

SQL expressions and strings can be executed via the:

Session = sessionmaker ( bind = engine ) session = Session () # execute a string statement result = session . execute ( "select * from table where id=:id" , { 'id' : }) # execute a SQL expression construct result = session . execute ( select ([ mytable ]) . where ( mytable . c . id == ))

The current method:

connection = session . connection ()

The examples above deal with a instance, which is used to locate theproper context for the desired engine:

Session = sessionmaker () session = Session () # need to specify mapper or class when executing result = session . execute ( "select * from table where id=:id" , { 'id' : }, mapper = MyMappedClass ) result = session . execute ( select ([ mytable ], mytable . c . id == ), mapper = MyMappedClass ) connection = session . connection ( MyMappedClass )
Forcing NULL on a column with a default

The ORM considers any attribute that was never set on an object as adefault case; the attribute will be omitted from the INSERT statement:

class MyObject ( Base ): __tablename__ = 'my_table' id = Column ( Integer , primary_key = True ) data = Column ( String ( ), nullable = True ) obj = MyObject ( id = ) session . add ( obj ) session . commit () # INSERT with the 'data' column omitted; the database # itself will persist this as the NULL value

Omitting a column from the INSERT means that the column willhave the NULL value set, unless the column has a default set up,in which case the default value will be persisted. This holds trueboth from a pure SQL perspective with server-side defaults, as well as thebehavior of SQLAlchemys insert behavior with both client-side and server-sidedefaults:

class MyObject ( Base ): __tablename__ = 'my_table' id = Column ( Integer , primary_key = True ) data = Column ( String ( ), nullable = True , server_default = "default" ) obj = MyObject ( id = ) session . add ( obj ) session . commit () # INSERT with the 'data' column omitted; the database # itself will persist this as the value 'default'

However, in the ORM, even if one assigns the Python value None explicitlyto the object, this is treated the same as though the value were neverassigned:

class MyObject ( Base ): __tablename__ = 'my_table' id = Column ( Integer , primary_key = True ) data = Column ( String ( ), nullable = True , server_default = "default" ) obj = MyObject ( id = , data = None ) session . add ( obj ) session . commit () # INSERT with the 'data' column explicitly set to None; # the ORM still omits it from the statement and the # database will still persist this as the value 'default'

The above operation will persist into the data column theserver default value of "default" and not SQL NULL, even though None was passed; this is a long-standing behavior of the ORM that many applicationshold as an assumption.

So what if we want to actually put NULL into this column, even though thecolumn has a default value? There are two approaches. One is thaton a per-instance level, we assign the attribute using the SQL construct:

from sqlalchemy import null obj = MyObject ( id = , data = null ()) session . add ( obj ) session . commit () # INSERT with the 'data' column explicitly set as null(); # the ORM uses this directly, bypassing all client- # and server-side defaults, and the database will # persist this as the NULL value

The SQL construct always translates into the SQLNULL value being directly present in the target INSERT statement.

If wed like to be able to use the Python value None and have thisalso be persisted as NULL despite the presence of column defaults,we can configure this for the ORM using a Core-level modifier, which indicatesa type where the ORM should treat the value None the same as any othervalue and pass it through, rather than omitting it as a missing value:

class MyObject ( Base ): __tablename__ = 'my_table' id = Column ( Integer , primary_key = True ) data = Column ( String ( ) . evaluates_none (), # indicate that None should always be passed nullable = True , server_default = "default" ) obj = MyObject ( id = , data = None ) session . add ( obj ) session . commit () # INSERT with the 'data' column explicitly set to None; # the ORM uses this directly, bypassing all client- # and server-side defaults, and the database will # persist this as the NULL value

Evaluating None

The modifier is primarily intended tosignal a type where the Python value None is significant, the primaryexample being a JSON type which may want to persist the JSON null valuerather than SQL NULL. We are slightly repurposing it here in order tosignal to the ORM that wed like None to be passed into the type wheneverpresent, even though no special type-level behaviors are assigned to it.

New in version 1.1: added the methodin order to indicate that a None value should be treated as significant.

Partitioning Strategies
Simple Vertical Partitioning

Vertical partitioning places different kinds of objects, or different tables,across multiple databases:

engine1 = create_engine ( 'postgresql://db1' ) engine2 = create_engine ( 'postgresql://db2' ) Session = sessionmaker ( twophase = True ) # bind User operations to engine 1, Account operations to engine 2 Session . configure ( binds = { User : engine1 , Account : engine2 }) session = Session ()

Above, operations against either class will make usage of the linked to that class. Upon a flush operation, similar rules take placeto ensure each class is written to the right database.

The transactions among the multiple databases can optionally be coordinatedvia two phase commit, if the underlying backend supports it. See for an example.

Custom Vertical Partitioning

More comprehensive rule-based class-level partitioning can be built byoverriding the which delivers the following rules:

  1. Flush operations are delivered to the engine named master .
  2. Operations on objects that subclass MyOtherClass alloccur on the other engine.
  3. Read operations for all other classes occur on a randomchoice of the slave1 or slave2 database.
engines = { 'master' : create_engine ( "sqlite:///master.db" ), 'other' : create_engine ( "sqlite:///other.db" ), 'slave1' : create_engine ( "sqlite:///slave1.db" ), 'slave2' : create_engine ( "sqlite:///slave2.db" ), } from sqlalchemy.orm import Session , sessionmaker import random class RoutingSession ( Session ): def get_bind ( self , mapper = None , clause = None ): if mapper and issubclass ( mapper . class_ , MyOtherClass ): return engines [ 'other' ] elif self . _flushing : return engines [ 'master' ] else : return engines [ random . choice ([ 'slave1' , 'slave2' ]) ]
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «SQLAlchemy 1.1 Documentation»

Look at similar books to SQLAlchemy 1.1 Documentation. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «SQLAlchemy 1.1 Documentation»

Discussion, reviews of the book SQLAlchemy 1.1 Documentation and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.