Commit Graph

8043 Commits

Author SHA1 Message Date
Mike Bayer 7dcfd1e019 Allow join() to pick the best candidate from multiple froms/entities
Refactored :meth:`.Query.join` to further clarify the individual components
of structuring the join. This refactor adds the ability for
:meth:`.Query.join` to determine the most appropriate "left" side of the
join when there is more than one element in the FROM list or the query is
against multiple entities.  In particular this targets the regression we
saw in 🎫`4363` but is also of general use.   The codepaths within
:meth:`.Query.join` are now easier to follow and the error cases are
decided more specifically at an earlier point in the operation.

Fixes: #4365
Change-Id: I403f451243904a020ceab4c3f94bead550c7b2d5
2018-11-14 21:35:15 -05:00
mike bayer 6b84db83e0 Merge "Add new "all columns" naming convention tokens" 2018-11-11 01:58:18 +00:00
mike bayer 680835f815 Merge "Modernize deferred callable for many-to-one comparison" 2018-11-11 01:56:41 +00:00
Mike Bayer d5c2db437e Modernize deferred callable for many-to-one comparison
Improved the behavior of a relationship-bound many-to-one object expression
such that the retrieval of column values on the related object are now
resilient against the object being detached from its parent
:class:`.Session`, even if the attribute has been expired.  New features
within the :class:`.InstanceState` are used to memoize the last known value
of a particular column attribute before its expired, so that the expression
can still evaluate when the object is detached and expired at the same
time.  Error conditions are also improved using modern attribute state
features to produce more specific messages as needed.

To support the value being mutated while also being resilient towards
expiration, a new feature to InstanceState is added ._last_known_values
which holds onto the expired value when an individual key is expired.
Only takes effect specific to keys and InstanceState objects that
received a special instruction so this does not add to overall
memory/latency.

Fixes: #4359
Change-Id: Iff272e667bf741074549db550bf65348553ca8e7
2018-11-10 20:55:57 -05:00
Mike Bayer be70559584 Add new "all columns" naming convention tokens
Added new naming convention tokens ``column_0N_name``, ``column_0_N_name``,
etc., which will render the names / keys / labels for all columns referenced
by a particular constraint in a sequence.  In order to accommodate for the
length of such a naming convention, the SQL compiler's auto-truncation
feature now applies itself to constraint names as well, which creates a
shortened, deterministically generated name for the constraint that will
apply to a target backend without going over the character limit of that
backend.

Additional notes:

1. the SQLite dialect had a format_index method that was apparently not
used, removed.

2. the naming convention logic has been applying the foreign key
remote column spec to the naming convention, and not the actual
column name.  In the case where the referenced Table object uses
.key inside the columns and these are what ForeignKey() references,
the naming convention was doing the wrong thing.   The patch here
fixes this, however this isn't noted in the migration notes.

Fixes: #3989
Change-Id: Ib24f4754b886676096c480fc54b2e5c2463ac99a
2018-11-10 16:13:01 -05:00
Mike Bayer af159c5695 Use case insensitive matching on lower_case_table_names=1,2
Fixed regression caused by 🎫`4344` released in 1.2.13, where the fix
for MySQL 8.0's case sensitivity problem with referenced column names when
reflecting foreign key referents is worked around using the
``information_schema.columns`` view.  The workaround was failing on OSX /
``lower_case_table_names=2`` which produces non-matching casing for the
``information_schema.columns`` vs. that of ``SHOW CREATE TABLE``, so in
case-insensitive SQL modes case-insensitive matching is now used.

Fixes: #4361
Change-Id: I748549bc4c27fad6394593f8ec93fc22bfd01f6c
2018-11-09 23:22:22 -05:00
mike bayer 07de512bf1 Merge "Implement SQLite ON CONFLICT for constraints" 2018-11-09 22:43:20 +00:00
Denis Kataev de804d7245 Implement SQLite ON CONFLICT for constraints
Implemented the SQLite ``ON CONFLICT`` clause as understood at the DDL
level, e.g. for primary key, unique, and CHECK constraints as well as
specified on a :class:`.Column` to satisfy inline primary key and NOT NULL.
Pull request courtesy Denis Kataev.

Fixes: #4360
Change-Id: I4cd4bafa8fca41e3101c87dbbfe169741bbda3f4
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/431
2018-11-07 18:26:40 -05:00
Mike Bayer 1f13c8c833 Improve documentation re: Session.binds and partitioning strategies
Update documentation to include background on arbitrary superclass
usage, add full cross-linking between all related methods and parameters.
De-emphasize "twophase" and document that it is not well-supported
in drivers.

Change-Id: Id99894bb62cc506e896c9aa7c256e9f6e602243e
2018-11-06 15:13:03 -05:00
Mike Bayer 15ac07f7b6 Merge remote-tracking branch 'origin/pr/486'
Change-Id: I90596d4fc238b913146906db0ab700dd767c5615
2018-11-03 12:45:31 -04:00
Mike Bayer b4e4325a7c Merge remote-tracking branch 'origin/pr/450'
Change-Id: I50c0dd75199a34f68f9a2fa0f89208a887193969
2018-11-03 12:37:19 -04:00
mike bayer 4811e35fa3 Merge "Deannotate ORM columns in ColumnEntity" 2018-11-03 16:17:04 +00:00
Mike Bayer 88bfa1b89c Deannotate ORM columns in ColumnEntity
Fixed a minor performance issue which could in some cases add unnecessary
overhead to result fetching, involving the use of ORM columns and entities
that include those same columns at the same time within a query.  The issue
has to do with hash / eq overhead when referring to the column in different
ways.

Fixes: #4347
Change-Id: I191d4d1b1623898060a9accdfd186de16f89a6b7
2018-11-02 16:40:59 -04:00
Iwo Herka 9a29ae2267 Tweak code-style and readability in events/base.py
This includes a few low-key, syntax-level tweaks and:

1. Rewrite of the if-statment in `Events._accept_with`.
2. Property name change, i.e. from `dispatcher.dispatch_cls` to
`dispatcher.dispatch`. In this case postfix `_cls` is confusing as
the property is not a class, but an instance of one.
2018-11-02 19:06:25 +01:00
Mike Bayer bc04e63475 Add new NO_RAISE attribute flag and specify for m2o history load
Added new behavior to the lazy load that takes place when the "old" value of
a many-to-one is retrieved, such that exceptions which would be raised due
to either ``lazy="raise"`` or a detached session error are skipped.

Fixes: #4353
Change-Id: I6c6c77613e93061a909f5062b70b17e8913fc9ee
2018-11-02 09:23:24 -04:00
mike bayer bc7c212370 Merge "Implement __delete__" 2018-11-02 13:21:15 +00:00
Mike Bayer 2e2af8d918 Implement __delete__
A long-standing oversight in the ORM, the ``__delete__`` method for a many-
to-one relationship was non-functional, e.g. for an operation such as ``del
a.b``.  This is now implemented and is equivalent to setting the attribute
to ``None``.

Fixes: #4354
Change-Id: I60131a84c007b0bf6f20c5cc5f21a3b96e954046
2018-11-02 01:22:41 -04:00
Mike Bayer e991684a39 Use attr keys when testing bulk update params for primary key
Fixed bug in :meth:`.Session.bulk_update_mappings` where alternate mapped
attribute names would result in the primary key column of the UPDATE
statement being included in the SET clause, as well as the WHERE clause;
while usually harmless, for SQL Server this can raise an error due to the
IDENTITY column.  This is a continuation of the same bug that was fixed in
🎫`.3849`, where testing was insufficient to catch this additional
flaw.

Fixes: #4357
Change-Id: Iead058c0465dfa31c5b8a8780769278b7000acc8
2018-11-01 21:54:10 -04:00
Chris Macklin 1c3b2d7186 Add missing index method to _AssociationList.
Added missing ``.index()`` method to list-based association collections
in the association proxy extension.

Change-Id: Ice81dc4bcccd520638c5bc9a0f8bb2027946c846
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/485
2018-10-31 11:29:26 -04:00
mike bayer abb12a9d81 Merge "Move pk on single-inh subclass check below conflict resolution check" 2018-10-31 15:24:55 +00:00
mike bayer 4c487b9477 Merge "Add reserved word 'function' for MySQL" 2018-10-30 21:23:49 +00:00
Tom Manderson 3ed79a5c18 Move pk on single-inh subclass check below conflict resolution check
The column conflict resolution technique discussed at
:ref:`declarative_column_conflicts` is now functional for a :class:`.Column`
that is also a primary key column.  Previously, a check for primary key
columns declared on a single-inheritance subclass would occur before the
column copy were allowed to pass.

Fixes: #4352
Change-Id: Id4c025da53c28e58db6b549fe398f25f8a90d355
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/483
2018-10-30 13:21:52 -04:00
Alex Hall 38c81328e9 Add reserved word 'function' for MySQL
Added word ``function`` to the list of reserved words for MySQL, which is
now a keyword in MySQL 8.0

Fixes: #4348
Change-Id: Idd30acda7e99076810f65d0ee860055a18dc9193
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/481
2018-10-30 13:12:13 -04:00
wodim f041f1d198 Add missing space 2018-10-30 13:04:36 -04:00
David Poggi 83864ffe63 re-word recycle parameter documentation
Change-Id: I80cabcd9fa3f3b45e5355bf6c774a8eee02e7f1b
2018-10-30 13:02:45 -04:00
Mike Bayer 65af4fc74a Merge remote-tracking branch 'origin/pr/478'
Change-Id: I0f27b6426dbfd665edf2a119b1cfaadb54511dd0
2018-10-30 12:53:42 -04:00
Mike Bayer 90574aabef Create object- and column-oriented versions of AssociationProxyInstance
The :class:`.AssociationProxy` now has standard column comparison operations
such as :meth:`.ColumnOperators.like` and
:meth:`.ColumnOperators.startswith` available when the target attribute is a
plain column - the EXISTS expression that joins to the target table is
rendered as usual, but the column expression is then use within the WHERE
criteria of the EXISTS.  Note that this alters the behavior of the
``.contains()`` method on the association proxy to make use of
:meth:`.ColumnOperators.contains` when used on a column-based attribute.

Fixes: #4351
Change-Id: I310941f4e8f778c200f8144a26a89e5364cd4dfb
2018-10-26 16:34:33 -04:00
Mike Bayer 1963fdceae - also correct for dbapi is a Mock without __version__...
Change-Id: I9e6b61a3292a2a6c8bb281c2375f2326282bcb76
2018-10-20 17:52:39 -04:00
mike bayer ead6fee1a7 Merge "Check more specifically for hybrid attr and not mapped property" 2018-10-20 16:55:01 -04:00
Mike Bayer 3cd9c81c1c - correct for dbapi not present in some tests
Change-Id: Iaa81a00658060d40add26c95cc69cee7edd5966a
2018-10-20 16:04:35 -04:00
Mike Bayer 801b37bd2a Enable decimal implicit bind for mysqlclient, is fixed as of
post 1.3.13

Change-Id: Ic7a2055597d06038ab330f1114416e4538964a2b
2018-10-20 12:32:25 -04:00
Mike Bayer 9335c24d6c Check more specifically for hybrid attr and not mapped property
Fixed regression caused by 🎫`4326` in version 1.2.12 where using
:class:`.declared_attr` with a mixin in conjunction with
:func:`.orm.synonym` would fail to map the synonym properly to an inherited
subclass.

Fixes: #4350
Change-Id: Ib2a9b6a125a2ac7c7ff80201746b7f10e5596226
2018-10-19 17:12:26 -04:00
Mike Bayer d080aae128 Add prop.secondary to FROM for dynamic loader
Fixed bug where "dynamic" loader needs to explicitly set the "secondary"
table in the FROM clause of the query, to suit the case where the secondary
is a join object that is otherwise not pulled into the query from its
columns alone.

Fixes: #4349
Change-Id: I397f62abd5603efa4fb273586d0f772bf8c8fbbf
2018-10-17 11:50:52 -04:00
Mike Bayer a8781b51b4 Don't call rollback on DBAPI connection that's "closed"
Use the existence of ConnectionRecord.connection to estimate
that this connection is likely closed, and if so, don't
try to call "rollback" on it.  This rollback is normally harmless
but is causing segfaults in mysqlclient due to
https://github.com/PyMySQL/mysqlclient-python/issues/270.

Change-Id: I1d7c5f5a520527d8268b6334795c2051f7ceeea6
2018-10-14 15:55:46 -04:00
Jayson Reis 21fbb5e38f selectinload omit join
The "selectin" loader strategy now omits the JOIN in the case of a
simple one-to-many load, where it instead relies upon the foreign key
columns of the related table in order to match up to primary keys in
the parent table.   This optimization can be disabled by setting
the :paramref:`.relationship.omit_join` flag to False.
Many thanks to Jayson Reis for the efforts on this.

As part of this change, horizontal shard no longer relies upon
the _mapper_zero() method to get the query-bound mapper, instead
using the more generalized _bind_mapper() (which will use mapper_zero
if no explicit FROM is present).  A short check for the particular
recursive condition is added to BundleEntity and it no longer assigns
itself as the "namespace" to its ColumnEntity objects which creates
a reference cycle.

Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com>
Fixes: #4340
Change-Id: I649587e1c07b684ecd63f7d10054cd165891baf4
Pull-request: https://bitbucket.org/zzzeek/sqlalchemy/pull-requests/7
2018-10-10 13:28:02 -04:00
mike bayer bb65193bff Merge "Route bulk update/delete exec through new Query._execute_crud method" 2018-10-04 15:53:51 -04:00
Mike Bayer 3081269e6f Route bulk update/delete exec through new Query._execute_crud method
Added support for bulk :meth:`.Query.update` and :meth:`.Query.delete`
to the :class:`.ShardedQuery` class within the horiziontal sharding
extension.  This also adds an additional expansion hook to the
bulk update/delete methods :meth:`.Query._execute_crud`.

Fixes: #4196
Change-Id: I65f56458176497a8cbdd368f41b879881f06348b
2018-10-04 13:59:41 -04:00
mike bayer 96bb76222e Merge "Support tuples of heterogeneous types for empty expanding IN" 2018-10-04 13:49:42 -04:00
James Owen 681bd7eb88 Add warning to hybrid property expression and fixup crosslinks 2018-10-04 17:12:41 +02:00
Mike Bayer aa21284270 Support tuples of heterogeneous types for empty expanding IN
Pass a list of all the types for the left side of an
IN expression to the visit_empty_set_expr() method, so that
the "empty expanding IN" can produce clauses for each element.

Fixes: #4271
Change-Id: I2738b9df2292ac01afda37f16d4fa56ae7bf9147
2018-10-03 10:40:38 -04:00
Mike Bayer 56fb68ca86 Perform additional retrieval of correct column names
Added a workaround for a MySQL bug #88718 introduced in the 8.0 series,
where the reflection of a foreign key constraint is not reporting the
correct case sensitivity for the referred column, leading to errors during
use of the reflected constraint such as when using the automap extension.
The workaround emits an additional query to the information_schema tables in
order to retrieve the correct case sensitive name.

Fixes: #4344
Change-Id: I08020d6eec43cbe8a56316660380d3739a0b45f7
2018-10-03 09:52:38 -04:00
mike bayer 2b9ba4049e Merge "Fix dependency_for final argument" 2018-10-02 09:10:24 -04:00
mike bayer acebd2df11 Merge "Add .info to InstanceState" 2018-10-01 21:37:49 -04:00
mike bayer 76963dc49c Merge "Add reflection support for Postgresql partitioned tables" 2018-10-01 21:36:47 -04:00
mike bayer d9c6bbbd94 Merge "Strong reference parent object in association proxy" 2018-10-01 21:34:05 -04:00
Joe Urciuoli cacc3c2057 Fix dependency_for final argument
Fixed issue where part of the utility language helper internals was passing
the wrong kind of argument to the Python ``__import__`` builtin as the list
of modules to be imported.  The issue produced no symptoms within the core
library but could cause issues with external applications that redefine the
``__import__`` builtin or otherwise instrument it. Pull request courtesy Joe
Urciuoli.

Per the submitter: "The fourth argument provided to `__import__`  (which
`import_` feeds in to) is supposed to be a a list of strings, but this code is
passing a single string. This was causing the sqlalchemy `import_` function to
break the string (for example 'interfaces') into an array of single characters
['i', 'n', ...], which causes the actual `__import__` to not find the module
`sqlalchemy.orm.i` (since it's trying to import `sqlalchemy.orm.i` and
`sqlalchemy.orm.n` .. etc)"

No issue could be reproduced locally as it seems you can put anything non-
empty/None into that last argument, even a list like ``['X']``, and  all the
sub-modules seem to appear.  Omit it, and then the sub-modules aren't present.
Perhaps it just runs the module or not if this attribute is present.

Change-Id: Ia15c74620f24d24f0df4882f9b36a04e2c3725b8
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/473
2018-10-01 16:57:51 -04:00
Mike Bayer 11947c3f1f Strong reference parent object in association proxy
Considering the reversal of #597 as well as
84420a1d0f as I am unable to reproduce
the original issues from that release.

The long-standing behavior of the association proxy collection maintaining
only a weak reference to the parent object is reverted; the proxy will now
maintain a strong reference to the parent for as long as the proxy
collection itself is also in memory, eliminating the "stale association
proxy" error. This change is being made on an experimental basis to see if
any use cases arise where it causes side effects.

Change-Id: I051334be90a343dd0e8a1f35e072075eb14b14a7
Fixes: #4268
2018-10-01 16:54:29 -04:00
Mike Bayer b171a2ed03 Add .info to InstanceState
Added ``.info`` dictionary to the :class:`.InstanceState` class, the object
that comes from calling :func:`.inspect` on a mapped object.

Fixes: #4257
Change-Id: I32d043f369edb708a17eec2e0b8876db0c1891b4
2018-10-01 16:35:27 -04:00
Mike Bayer f1ca155cea Add reflection support for Postgresql partitioned tables
Added rudimental support for reflection of Postgresql
partitioned tables, e.g. that relkind='p' is added to reflection
queries that return table information.

Fixes: #4237
Change-Id: I66fd10b002e4ed21ea13b13a7e35a85f66bdea75
2018-10-01 16:24:55 -04:00
Mike Bayer 313879d6ad Add test and retroactive changelog for issue 4040
Fixes: #4040
Change-Id: I707c1cd2708a37102ad8184bec21be35cb6242d7
2018-10-01 14:00:39 -04:00