overhaul metadata table storage, apply to declarative

Issue #1893 resolved
Mike Bayer repo owner created an issue
diff -r 1402a608ebc28031ce11dec9ed6a90785f09294d lib/sqlalchemy/ext/declarative.py
--- a/lib/sqlalchemy/ext/declarative.py Wed Aug 25 11:01:46 2010 -0400
+++ b/lib/sqlalchemy/ext/declarative.py Wed Aug 25 19:19:35 2010 -0400
@@ -1173,6 +1173,8 @@
                 return _GetColumns(cls._decl_class_registry[key](key))
             elif key in cls.metadata.tables:
                 return cls.metadata.tables[key](key)
+            elif key.replace("_", ".") in cls.metadata.tables:
+                return cls.metadata.tables[".")](key.replace("_",)
             else:
                 return sqlalchemy.__dict__[key](key)

Comments (5)

  1. Mike Bayer reporter

    so attached is a patch that hacks this out in a way I don't really like. Here's what I want, and it will be 0.7:

    m = MetaData()
    
    t1 = Table('foo', m, ..., schema="x")
    t2 = Table('bar', m, ...)
    
    assert m.tables.bar is t2
    assert m.tables.x.foo is t1
    
    # old interface works for reads:
    assert m.tables['bar']('bar') is t2
    assert m.tables['x.foo']('x.foo') is t1
    
    # and this
    assert m.tables['x']('x')['foo']('foo') is t1
    

    So ".tables" becomes just like ".columns". Its a dict/prop interface that is definitely not mutable (its almost like OrderedProperties but I don't want it to have an ordering to start and i really dont want any mutators on it, so make it something new). remove() as always will remove a table.

  2. Mike Bayer reporter

    unfortunately that idea breaks for tablename + schemaname are the same. so next idea:

    m.schemas.x.foo
    m.tables.bar
    

    for declarative, not sure if there's any real solution here.

  3. Mike Bayer reporter

    Patch has been updated, leaving mostly the original scheme in place, except metadata.tables becomes a frozendict. Changing my mind regarding fancy schema.table accessors for now.

  4. Log in to comment