server/schema2sql.py
changeset 11005 f8417bd135ed
parent 11003 53820b08a723
child 11287 b537c07e3bdc
--- a/server/schema2sql.py	Wed Dec 09 17:44:17 2015 +0100
+++ b/server/schema2sql.py	Wed Dec 09 17:44:18 2015 +0100
@@ -190,8 +190,7 @@
     """write an attribute schema as SQL statements to stdout"""
     attr = rschema.type
     rdef = rschema.rdef(eschema.type, aschema.type)
-    sqltype = type_from_constraints(dbhelper, aschema.type, rdef.constraints,
-                                    creating)
+    sqltype = type_from_rdef(dbhelper, rdef, creating)
     if SET_DEFAULT:
         default = eschema.default(attr)
         if default is not None:
@@ -215,11 +214,11 @@
     return sqltype
 
 
-def type_from_constraints(dbhelper, etype, constraints, creating=True):
-    """return a sql type string corresponding to the constraints"""
-    constraints = list(constraints)
+def type_from_rdef(dbhelper, rdef, creating=True):
+    """return a sql type string corresponding to the relation definition"""
+    constraints = list(rdef.constraints)
     unique, sqltype = False, None
-    if etype == 'String':
+    if rdef.object.type == 'String':
         for constraint in constraints:
             if isinstance(constraint, SizeConstraint):
                 if constraint.max is not None:
@@ -229,12 +228,19 @@
             elif isinstance(constraint, UniqueConstraint):
                 unique = True
     if sqltype is None:
-        sqltype = dbhelper.TYPE_MAPPING[etype]
+        sqltype = sql_type(dbhelper, rdef)
     if creating and unique:
         sqltype += ' UNIQUE'
     return sqltype
 
 
+def sql_type(dbhelper, rdef):
+    sqltype = dbhelper.TYPE_MAPPING[rdef.object]
+    if callable(sqltype):
+        sqltype = sqltype(rdef)
+    return sqltype
+
+
 _SQL_SCHEMA = """
 CREATE TABLE %(table)s (
   eid_from INTEGER NOT NULL REFERENCES entities (eid),