1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
|
=============================
What's new in SQLAlchemy 0.4?
=============================
.. admonition:: About this Document
This document describes changes between SQLAlchemy version 0.3,
last released October 14, 2007, and SQLAlchemy version 0.4,
last released October 12, 2008.
Document date: March 21, 2008
First Things First
==================
If you're using any ORM features, make sure you import from
``sqlalchemy.orm``:
::
from sqlalchemy import *
from sqlalchemy.orm import *
Secondly, anywhere you used to say ``engine=``,
``connectable=``, ``bind_to=``, ``something.engine``,
``metadata.connect()``, use ``bind``:
::
myengine = create_engine('sqlite://')
meta = MetaData(myengine)
meta2 = MetaData()
meta2.bind = myengine
session = create_session(bind=myengine)
statement = select([table], bind=myengine)
Got those ? Good! You're now (95%) 0.4 compatible. If
you're using 0.3.10, you can make these changes immediately;
they'll work there too.
Module Imports
==============
In 0.3, "``from sqlachemy import *``" would import all of
sqlachemy's sub-modules into your namespace. Version 0.4 no
longer imports sub-modules into the namespace. This may mean
you need to add extra imports into your code.
In 0.3, this code worked:
::
from sqlalchemy import *
class UTCDateTime(types.TypeDecorator):
pass
In 0.4, one must do:
::
from sqlalchemy import *
from sqlalchemy import types
class UTCDateTime(types.TypeDecorator):
pass
Object Relational Mapping
=========================
Querying
--------
New Query API
^^^^^^^^^^^^^
Query is standardized on the generative interface (old
interface is still there, just deprecated). While most of
the generative interface is available in 0.3, the 0.4 Query
has the inner guts to match the generative outside, and has
a lot more tricks. All result narrowing is via ``filter()``
and ``filter_by()``, limiting/offset is either through array
slices or ``limit()``/``offset()``, joining is via
``join()`` and ``outerjoin()`` (or more manually, through
``select_from()`` as well as manually-formed criteria).
To avoid deprecation warnings, you must make some changes to
your 03 code
User.query.get_by( \**kwargs )
::
User.query.filter_by(**kwargs).first()
User.query.select_by( \**kwargs )
::
User.query.filter_by(**kwargs).all()
User.query.select()
::
User.query.filter(xxx).all()
New Property-Based Expression Constructs
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
By far the most palpable difference within the ORM is that
you can now construct your query criterion using class-based
attributes directly. The ".c." prefix is no longer needed
when working with mapped classes:
::
session.query(User).filter(and_(User.name == 'fred', User.id > 17))
While simple column-based comparisons are no big deal, the
class attributes have some new "higher level" constructs
available, including what was previously only available in
``filter_by()``:
::
# comparison of scalar relations to an instance
filter(Address.user == user)
# return all users who contain a particular address
filter(User.addresses.contains(address))
# return all users who *dont* contain the address
filter(~User.address.contains(address))
# return all users who contain a particular address with
# the email_address like '%foo%'
filter(User.addresses.any(Address.email_address.like('%foo%')))
# same, email address equals 'foo@bar.com'. can fall back to keyword
# args for simple comparisons
filter(User.addresses.any(email_address = 'foo@bar.com'))
# return all Addresses whose user attribute has the username 'ed'
filter(Address.user.has(name='ed'))
# return all Addresses whose user attribute has the username 'ed'
# and an id > 5 (mixing clauses with kwargs)
filter(Address.user.has(User.id > 5, name='ed'))
The ``Column`` collection remains available on mapped
classes in the ``.c`` attribute. Note that property-based
expressions are only available with mapped properties of
mapped classes. ``.c`` is still used to access columns in
regular tables and selectable objects produced from SQL
Expressions.
Automatic Join Aliasing
^^^^^^^^^^^^^^^^^^^^^^^
We've had join() and outerjoin() for a while now:
::
session.query(Order).join('items')...
Now you can alias them:
::
session.query(Order).join('items', aliased=True).
filter(Item.name='item 1').join('items', aliased=True).filter(Item.name=='item 3')
The above will create two joins from orders->items using
aliases. the ``filter()`` call subsequent to each will
adjust its table criterion to that of the alias. To get at
the ``Item`` objects, use ``add_entity()`` and target each
join with an ``id``:
::
session.query(Order).join('items', id='j1', aliased=True).
filter(Item.name == 'item 1').join('items', aliased=True, id='j2').
filter(Item.name == 'item 3').add_entity(Item, id='j1').add_entity(Item, id='j2')
Returns tuples in the form: ``(Order, Item, Item)``.
Self-referential Queries
^^^^^^^^^^^^^^^^^^^^^^^^
So query.join() can make aliases now. What does that give
us ? Self-referential queries ! Joins can be done without
any ``Alias`` objects:
::
# standard self-referential TreeNode mapper with backref
mapper(TreeNode, tree_nodes, properties={
'children':relation(TreeNode, backref=backref('parent', remote_side=tree_nodes.id))
})
# query for node with child containing "bar" two levels deep
session.query(TreeNode).join(["children", "children"], aliased=True).filter_by(name='bar')
To add criterion for each table along the way in an aliased
join, you can use ``from_joinpoint`` to keep joining against
the same line of aliases:
::
# search for the treenode along the path "n1/n12/n122"
# first find a Node with name="n122"
q = sess.query(Node).filter_by(name='n122')
# then join to parent with "n12"
q = q.join('parent', aliased=True).filter_by(name='n12')
# join again to the next parent with 'n1'. use 'from_joinpoint'
# so we join from the previous point, instead of joining off the
# root table
q = q.join('parent', aliased=True, from_joinpoint=True).filter_by(name='n1')
node = q.first()
``query.populate_existing()``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The eager version of ``query.load()`` (or
``session.refresh()``). Every instance loaded from the
query, including all eagerly loaded items, get refreshed
immediately if already present in the session:
::
session.query(Blah).populate_existing().all()
Relations
---------
SQL Clauses Embedded in Updates/Inserts
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
For inline execution of SQL clauses, embedded right in the
UPDATE or INSERT, during a ``flush()``:
::
myobject.foo = mytable.c.value + 1
user.pwhash = func.md5(password)
order.hash = text("select hash from hashing_table")
The column-attribute is set up with a deferred loader after
the operation, so that it issues the SQL to load the new
value when you next access.
Self-referential and Cyclical Eager Loading
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Since our alias-fu has improved, ``relation()`` can join
along the same table \*any number of times*; you tell it how
deep you want to go. Lets show the self-referential
``TreeNode`` more clearly:
::
nodes = Table('nodes', metadata,
Column('id', Integer, primary_key=True),
Column('parent_id', Integer, ForeignKey('nodes.id')),
Column('name', String(30)))
class TreeNode(object):
pass
mapper(TreeNode, nodes, properties={
'children':relation(TreeNode, lazy=False, join_depth=3)
})
So what happens when we say:
::
create_session().query(TreeNode).all()
? A join along aliases, three levels deep off the parent:
::
SELECT
nodes_3.id AS nodes_3_id, nodes_3.parent_id AS nodes_3_parent_id, nodes_3.name AS nodes_3_name,
nodes_2.id AS nodes_2_id, nodes_2.parent_id AS nodes_2_parent_id, nodes_2.name AS nodes_2_name,
nodes_1.id AS nodes_1_id, nodes_1.parent_id AS nodes_1_parent_id, nodes_1.name AS nodes_1_name,
nodes.id AS nodes_id, nodes.parent_id AS nodes_parent_id, nodes.name AS nodes_name
FROM nodes LEFT OUTER JOIN nodes AS nodes_1 ON nodes.id = nodes_1.parent_id
LEFT OUTER JOIN nodes AS nodes_2 ON nodes_1.id = nodes_2.parent_id
LEFT OUTER JOIN nodes AS nodes_3 ON nodes_2.id = nodes_3.parent_id
ORDER BY nodes.oid, nodes_1.oid, nodes_2.oid, nodes_3.oid
Notice the nice clean alias names too. The joining doesn't
care if it's against the same immediate table or some other
object which then cycles back to the beginning. Any kind
of chain of eager loads can cycle back onto itself when
``join_depth`` is specified. When not present, eager
loading automatically stops when it hits a cycle.
Composite Types
^^^^^^^^^^^^^^^
This is one from the Hibernate camp. Composite Types let
you define a custom datatype that is composed of more than
one column (or one column, if you wanted). Lets define a
new type, ``Point``. Stores an x/y coordinate:
::
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __composite_values__(self):
return self.x, self.y
def __eq__(self, other):
return other.x == self.x and other.y == self.y
def __ne__(self, other):
return not self.__eq__(other)
The way the ``Point`` object is defined is specific to a
custom type; constructor takes a list of arguments, and the
``__composite_values__()`` method produces a sequence of
those arguments. The order will match up to our mapper, as
we'll see in a moment.
Let's create a table of vertices storing two points per row:
::
vertices = Table('vertices', metadata,
Column('id', Integer, primary_key=True),
Column('x1', Integer),
Column('y1', Integer),
Column('x2', Integer),
Column('y2', Integer),
)
Then, map it ! We'll create a ``Vertex`` object which
stores two ``Point`` objects:
::
class Vertex(object):
def __init__(self, start, end):
self.start = start
self.end = end
mapper(Vertex, vertices, properties={
'start':composite(Point, vertices.c.x1, vertices.c.y1),
'end':composite(Point, vertices.c.x2, vertices.c.y2)
})
Once you've set up your composite type, it's usable just
like any other type:
::
v = Vertex(Point(3, 4), Point(26,15))
session.save(v)
session.flush()
# works in queries too
q = session.query(Vertex).filter(Vertex.start == Point(3, 4))
If you'd like to define the way the mapped attributes
generate SQL clauses when used in expressions, create your
own ``sqlalchemy.orm.PropComparator`` subclass, defining any
of the common operators (like ``__eq__()``, ``__le__()``,
etc.), and send it in to ``composite()``. Composite types
work as primary keys too, and are usable in ``query.get()``:
::
# a Document class which uses a composite Version
# object as primary key
document = query.get(Version(1, 'a'))
``dynamic_loader()`` relations
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A ``relation()`` that returns a live ``Query`` object for
all read operations. Write operations are limited to just
``append()`` and ``remove()``, changes to the collection are
not visible until the session is flushed. This feature is
particularly handy with an "autoflushing" session which will
flush before each query.
::
mapper(Foo, foo_table, properties={
'bars':dynamic_loader(Bar, backref='foo', <other relation() opts>)
})
session = create_session(autoflush=True)
foo = session.query(Foo).first()
foo.bars.append(Bar(name='lala'))
for bar in foo.bars.filter(Bar.name=='lala'):
print(bar)
session.commit()
New Options: ``undefer_group()``, ``eagerload_all()``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A couple of query options which are handy.
``undefer_group()`` marks a whole group of "deferred"
columns as undeferred:
::
mapper(Class, table, properties={
'foo' : deferred(table.c.foo, group='group1'),
'bar' : deferred(table.c.bar, group='group1'),
'bat' : deferred(table.c.bat, group='group1'),
)
session.query(Class).options(undefer_group('group1')).filter(...).all()
and ``eagerload_all()`` sets a chain of attributes to be
eager in one pass:
::
mapper(Foo, foo_table, properties={
'bar':relation(Bar)
})
mapper(Bar, bar_table, properties={
'bat':relation(Bat)
})
mapper(Bat, bat_table)
# eager load bar and bat
session.query(Foo).options(eagerload_all('bar.bat')).filter(...).all()
New Collection API
^^^^^^^^^^^^^^^^^^
Collections are no longer proxied by an
{{{InstrumentedList}}} proxy, and access to members, methods
and attributes is direct. Decorators now intercept objects
entering and leaving the collection, and it is now possible
to easily write a custom collection class that manages its
own membership. Flexible decorators also replace the named
method interface of custom collections in 0.3, allowing any
class to be easily adapted to use as a collection container.
Dictionary-based collections are now much easier to use and
fully ``dict``-like. Changing ``__iter__`` is no longer
needed for ``dict``s, and new built-in ``dict`` types cover
many needs:
::
# use a dictionary relation keyed by a column
relation(Item, collection_class=column_mapped_collection(items.c.keyword))
# or named attribute
relation(Item, collection_class=attribute_mapped_collection('keyword'))
# or any function you like
relation(Item, collection_class=mapped_collection(lambda entity: entity.a + entity.b))
Existing 0.3 ``dict``-like and freeform object derived
collection classes will need to be updated for the new API.
In most cases this is simply a matter of adding a couple
decorators to the class definition.
Mapped Relations from External Tables/Subqueries
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This feature quietly appeared in 0.3 but has been improved
in 0.4 thanks to better ability to convert subqueries
against a table into subqueries against an alias of that
table; this is key for eager loading, aliased joins in
queries, etc. It reduces the need to create mappers against
select statements when you just need to add some extra
columns or subqueries:
::
mapper(User, users, properties={
'fullname': column_property((users.c.firstname + users.c.lastname).label('fullname')),
'numposts': column_property(
select([func.count(1)], users.c.id==posts.c.user_id).correlate(users).label('posts')
)
})
a typical query looks like:
::
SELECT (SELECT count(1) FROM posts WHERE users.id = posts.user_id) AS count,
users.firstname || users.lastname AS fullname,
users.id AS users_id, users.firstname AS users_firstname, users.lastname AS users_lastname
FROM users ORDER BY users.oid
Horizontal Scaling (Sharding) API
---------------------------------
[browser:/sqlalchemy/trunk/examples/sharding/attribute_shard
.py]
Sessions
--------
New Session Create Paradigm; SessionContext, assignmapper Deprecated
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
That's right, the whole shebang is being replaced with two
configurational functions. Using both will produce the most
0.1-ish feel we've had since 0.1 (i.e., the least amount of
typing).
Configure your own ``Session`` class right where you define
your ``engine`` (or anywhere):
::
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine('myengine://')
Session = sessionmaker(bind=engine, autoflush=True, transactional=True)
# use the new Session() freely
sess = Session()
sess.save(someobject)
sess.flush()
If you need to post-configure your Session, say with an
engine, add it later with ``configure()``:
::
Session.configure(bind=create_engine(...))
All the behaviors of ``SessionContext`` and the ``query``
and ``__init__`` methods of ``assignmapper`` are moved into
the new ``scoped_session()`` function, which is compatible
with both ``sessionmaker`` as well as ``create_session()``:
::
from sqlalchemy.orm import scoped_session, sessionmaker
Session = scoped_session(sessionmaker(autoflush=True, transactional=True))
Session.configure(bind=engine)
u = User(name='wendy')
sess = Session()
sess.save(u)
sess.commit()
# Session constructor is thread-locally scoped. Everyone gets the same
# Session in the thread when scope="thread".
sess2 = Session()
assert sess is sess2
When using a thread-local ``Session``, the returned class
has all of ``Session's`` interface implemented as
classmethods, and "assignmapper"'s functionality is
available using the ``mapper`` classmethod. Just like the
old ``objectstore`` days....
::
# "assignmapper"-like functionality available via ScopedSession.mapper
Session.mapper(User, users_table)
u = User(name='wendy')
Session.commit()
Sessions are again Weak Referencing By Default
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The weak_identity_map flag is now set to ``True`` by default
on Session. Instances which are externally deferenced and
fall out of scope are removed from the session
automatically. However, items which have "dirty" changes
present will remain strongly referenced until those changes
are flushed at which case the object reverts to being weakly
referenced (this works for 'mutable' types, like picklable
attributes, as well). Setting weak_identity_map to
``False`` restores the old strong-referencing behavior for
those of you using the session like a cache.
Auto-Transactional Sessions
^^^^^^^^^^^^^^^^^^^^^^^^^^^
As you might have noticed above, we are calling ``commit()``
on ``Session``. The flag ``transactional=True`` means the
``Session`` is always in a transaction, ``commit()``
persists permanently.
Auto-Flushing Sessions
^^^^^^^^^^^^^^^^^^^^^^
Also, ``autoflush=True`` means the ``Session`` will
``flush()`` before each ``query`` as well as when you call
``flush()`` or ``commit()``. So now this will work:
::
Session = sessionmaker(bind=engine, autoflush=True, transactional=True)
u = User(name='wendy')
sess = Session()
sess.save(u)
# wendy is flushed, comes right back from a query
wendy = sess.query(User).filter_by(name='wendy').one()
Transactional methods moved onto sessions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
``commit()`` and ``rollback()``, as well as ``begin()`` are
now directly on ``Session``. No more need to use
``SessionTransaction`` for anything (it remains in the
background).
::
Session = sessionmaker(autoflush=True, transactional=False)
sess = Session()
sess.begin()
# use the session
sess.commit() # commit transaction
Sharing a ``Session`` with an enclosing engine-level (i.e.
non-ORM) transaction is easy:
::
Session = sessionmaker(autoflush=True, transactional=False)
conn = engine.connect()
trans = conn.begin()
sess = Session(bind=conn)
# ... session is transactional
# commit the outermost transaction
trans.commit()
Nested Session Transactions with SAVEPOINT
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Available at the Engine and ORM level. ORM docs so far:
http://www.sqlalchemy.org/docs/04/session.html#unitofwork_ma
naging
Two-Phase Commit Sessions
^^^^^^^^^^^^^^^^^^^^^^^^^
Available at the Engine and ORM level. ORM docs so far:
http://www.sqlalchemy.org/docs/04/session.html#unitofwork_ma
naging
Inheritance
-----------
Polymorphic Inheritance with No Joins or Unions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
New docs for inheritance: http://www.sqlalchemy.org/docs/04
/mappers.html#advdatamapping_mapper_inheritance_joined
Better Polymorphic Behavior with ``get()``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
All classes within a joined-table inheritance hierarchy get
an ``_instance_key`` using the base class, i.e.
``(BaseClass, (1, ), None)``. That way when you call
``get()`` a ``Query`` against the base class, it can locate
subclass instances in the current identity map without
querying the database.
Types
-----
Custom Subclasses of ``sqlalchemy.types.TypeDecorator``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
There is a `New API <http://www.sqlalchemy.org/docs/04/types
.html#types_custom>`_ for subclassing a TypeDecorator.
Using the 0.3 API causes compilation errors in some cases.
SQL Expressions
===============
All New, Deterministic Label/Alias Generation
---------------------------------------------
All the "anonymous" labels and aliases use a simple
<name>_<number> format now. SQL is much easier to read and
is compatible with plan optimizer caches. Just check out
some of the examples in the tutorials:
http://www.sqlalchemy.org/docs/04/ormtutorial.html
http://www.sqlalchemy.org/docs/04/sqlexpression.html
Generative select() Constructs
------------------------------
This is definitely the way to go with ``select()``. See htt
p://www.sqlalchemy.org/docs/04/sqlexpression.html#sql_transf
orm .
New Operator System
-------------------
SQL operators and more or less every SQL keyword there is
are now abstracted into the compiler layer. They now act
intelligently and are type/backend aware, see: http://www.sq
lalchemy.org/docs/04/sqlexpression.html#sql_operators
All ``type`` Keyword Arguments Renamed to ``type_``
---------------------------------------------------
Just like it says:
::
b = bindparam('foo', type_=String)
in\_ Function Changed to Accept Sequence or Selectable
------------------------------------------------------
The in\_ function now takes a sequence of values or a
selectable as its sole argument. The previous API of passing
in values as positional arguments still works, but is now
deprecated. This means that
::
my_table.select(my_table.c.id.in_(1,2,3)
my_table.select(my_table.c.id.in_(*listOfIds)
should be changed to
::
my_table.select(my_table.c.id.in_([1,2,3])
my_table.select(my_table.c.id.in_(listOfIds)
Schema and Reflection
=====================
``MetaData``, ``BoundMetaData``, ``DynamicMetaData``...
-------------------------------------------------------
In the 0.3.x series, ``BoundMetaData`` and
``DynamicMetaData`` were deprecated in favor of ``MetaData``
and ``ThreadLocalMetaData``. The older names have been
removed in 0.4. Updating is simple:
::
+-------------------------------------+-------------------------+
|If You Had | Now Use |
+=====================================+=========================+
| ``MetaData`` | ``MetaData`` |
+-------------------------------------+-------------------------+
| ``BoundMetaData`` | ``MetaData`` |
+-------------------------------------+-------------------------+
| ``DynamicMetaData`` (with one | ``MetaData`` |
| engine or threadlocal=False) | |
+-------------------------------------+-------------------------+
| ``DynamicMetaData`` | ``ThreadLocalMetaData`` |
| (with different engines per thread) | |
+-------------------------------------+-------------------------+
The seldom-used ``name`` parameter to ``MetaData`` types has
been removed. The ``ThreadLocalMetaData`` constructor now
takes no arguments. Both types can now be bound to an
``Engine`` or a single ``Connection``.
One Step Multi-Table Reflection
-------------------------------
You can now load table definitions and automatically create
``Table`` objects from an entire database or schema in one
pass:
::
>>> metadata = MetaData(myengine, reflect=True)
>>> metadata.tables.keys()
['table_a', 'table_b', 'table_c', '...']
``MetaData`` also gains a ``.reflect()`` method enabling
finer control over the loading process, including
specification of a subset of available tables to load.
SQL Execution
=============
``engine``, ``connectable``, and ``bind_to`` are all now ``bind``
-----------------------------------------------------------------
``Transactions``, ``NestedTransactions`` and ``TwoPhaseTransactions``
---------------------------------------------------------------------
Connection Pool Events
----------------------
The connection pool now fires events when new DB-API
connections are created, checked out and checked back into
the pool. You can use these to execute session-scoped SQL
setup statements on fresh connections, for example.
Oracle Engine Fixed
-------------------
In 0.3.11, there were bugs in the Oracle Engine on how
Primary Keys are handled. These bugs could cause programs
that worked fine with other engines, such as sqlite, to fail
when using the Oracle Engine. In 0.4, the Oracle Engine has
been reworked, fixing these Primary Key problems.
Out Parameters for Oracle
-------------------------
::
result = engine.execute(text("begin foo(:x, :y, :z); end;", bindparams=[bindparam('x', Numeric), outparam('y', Numeric), outparam('z', Numeric)]), x=5)
assert result.out_parameters == {'y':10, 'z':75}
Connection-bound ``MetaData``, ``Sessions``
-------------------------------------------
``MetaData`` and ``Session`` can be explicitly bound to a
connection:
::
conn = engine.connect()
sess = create_session(bind=conn)
Faster, More Foolproof ``ResultProxy`` Objects
----------------------------------------------
|