]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Added basic math expression coercion for
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 13 Aug 2010 18:25:58 +0000 (14:25 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 13 Aug 2010 18:25:58 +0000 (14:25 -0400)
Numeric->Integer,
so that resulting type is Numeric regardless
of the direction of the expression.

CHANGES
lib/sqlalchemy/types.py
test/sql/test_types.py

diff --git a/CHANGES b/CHANGES
index 24bd97b288c42b2849fbb1203462550751aabe65..da9f3dca0ecb2a0581b1abd3d81e730726ff7d23 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -77,6 +77,11 @@ CHANGES
     the outer query.  [ticket:1852]
     
 - sql
+  - Added basic math expression coercion for 
+    Numeric->Integer,
+    so that resulting type is Numeric regardless
+    of the direction of the expression.
+    
   - Changed the scheme used to generate truncated
     "auto" index names when using the "index=True"
     flag on Column.   The truncation only takes
index 91eb352751ecf955ede8365059976e4fd1e953ae..777353714af839c722f431c9257f27a919fb792c 100644 (file)
@@ -1024,20 +1024,25 @@ class Numeric(_DateAffinity, TypeEngine):
             operators.mul:{
                 Interval:Interval,
                 Numeric:Numeric,
+                Integer:Numeric,
             },
             # Py2K
             operators.div:{
                 Numeric:Numeric,
+                Integer:Numeric,
             },
             # end Py2K
             operators.truediv:{
                 Numeric:Numeric,
+                Integer:Numeric,
             },
             operators.add:{
                 Numeric:Numeric,
+                Integer:Numeric,
             },
             operators.sub:{
                 Numeric:Numeric,
+                Integer:Numeric,
             }
         }
 
index 91e4ec177afbe1e5d093030b32e344a0c5102316..a80e761d7393d61a544fc48658a0e8980988a402 100644 (file)
@@ -904,6 +904,30 @@ class ExpressionTest(TestBase, AssertsExecutionResults, AssertsCompiledSQL):
         expr = func.current_date() - column('foo', types.TIMESTAMP)
         eq_(expr.type._type_affinity, types.Interval)
     
+    def test_numerics_coercion(self):
+        from sqlalchemy.sql import column
+        import operator
+        
+        for op in (
+            operator.add,
+            operator.mul,
+            operator.truediv,
+            operator.sub
+        ):
+            for other in (Numeric(10, 2), Integer):
+                expr = op(
+                        column('bar', types.Numeric(10, 2)),
+                        column('foo', other)
+                       )
+                assert isinstance(expr.type, types.Numeric)
+                expr = op(
+                        column('foo', other),
+                        column('bar', types.Numeric(10, 2))
+                       )
+                assert isinstance(expr.type, types.Numeric)
+
+        
+        
     def test_expression_typing(self):
         expr = column('bar', Integer) - 3