Re: [Sqlalchemy-tickets] [sqlalchemy] #2828: emit warning when Column is assigned directly to multi
Brought to you by:
zzzeek
|
From: sqlalchemy <mi...@zz...> - 2013-09-18 15:25:58
|
#2828: emit warning when Column is assigned directly to multiple names
------------------------------+-------------------------------
Reporter: jerryji | Owner: zzzeek
Type: defect | Status: new
Priority: medium | Milestone: 0.8.xx
Component: declarative | Severity: major - 1-3 hours
Resolution: | Keywords: column_property
Progress State: needs tests |
------------------------------+-------------------------------
Changes (by zzzeek):
* milestone: => 0.8.xx
* component: orm => declarative
* severity: no triage selected yet => major - 1-3 hours
* status_field: awaiting triage => needs tests
Comment:
there's no bug here, the declaration of `name = Column` is equivalent of
`name_alias = column_property(Column)`, and an explicit column property
trumps the plain column as you might be passing extra mapping options
along. We can emit a warning for when this occurs, this patch is a
start, needs some cleanup and tests:
{{{
#!python
diff --git a/lib/sqlalchemy/ext/declarative/base.py
b/lib/sqlalchemy/ext/declarative/base.py
index 820c087..73723bc 100644
--- a/lib/sqlalchemy/ext/declarative/base.py
+++ b/lib/sqlalchemy/ext/declarative/base.py
@@ -14,7 +14,7 @@ from ... import util, exc
from ...sql import expression
from ... import event
from . import clsregistry
-
+import collections
def _declared_mapping_info(cls):
# deferred mapping
@@ -173,15 +173,19 @@ def _as_declarative(cls, classname, dict_):
# extract columns from the class dict
declared_columns = set()
+ col_to_prop = collections.defaultdict(dict)
for key, c in list(our_stuff.items()):
if isinstance(c, (ColumnProperty, CompositeProperty)):
for col in c.columns:
if isinstance(col, Column) and \
col.table is None:
_undefer_column_name(key, col)
+ if isinstance(c, ColumnProperty):
+ col_to_prop[col][key] = c
declared_columns.add(col)
elif isinstance(c, Column):
_undefer_column_name(key, c)
+ col_to_prop[c][key] = c
declared_columns.add(c)
# if the column is the same name as the key,
# remove it from the explicit properties dict.
@@ -190,6 +194,10 @@ def _as_declarative(cls, classname, dict_):
# in multi-column ColumnProperties.
if key == c.key:
del our_stuff[key]
+ for col, entry in col_to_prop.items():
+ if len(entry) > 1:
+ util.warn("Column object named directly multiple times, "
+ "only one will be used: %s" % (", ".join(entry)))
declared_columns = sorted(
declared_columns, key=lambda c: c._creation_order)
table = None
diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py
index 4336c19..f828180 100644
--- a/lib/sqlalchemy/orm/mapper.py
+++ b/lib/sqlalchemy/orm/mapper.py
@@ -1218,7 +1218,6 @@ class Mapper(_InspectionAttr):
self._log("Identified primary key columns: %s", primary_key)
def _configure_properties(self):
-
# Column and other ClauseElement objects which are mapped
self.columns = self.c = util.OrderedProperties()
}}}
this also should probably try to detect if a straight Column is specified
multiple times.
in this use case you want to be using
[http://docs.sqlalchemy.org/en/rel_0_8/orm/mapper_config.html?highlight=synonym#sqlalchemy.orm.synonym
synonym] in order to produce a straight "alias" of a name.
--
Ticket URL: <http://www.sqlalchemy.org/trac/ticket/2828#comment:1>
sqlalchemy <http://www.sqlalchemy.org/>
The Database Toolkit for Python
|