On Apr 2, 2014, at 5:10 PM, Rob Crowell <rob.crow...@moat.com> wrote:

> I'm using the ORM and one of my tables does not have a primary key defined.  
> I am also using DeferredReflection, and I can't seem to figure out how to 
> defer the PrimaryKeyConstraint until Base.prepare() runs.  Any pointers?
> 
>     Base = declarative_base(cls=DeferredReflection)
> 
>     class Person(Base):
>         __tablename__ = 'people'
>         __table_args__ = (PrimaryKeyConstraint(u'name'), {})
> 
>     # this does not run
>     if __name__ == '__main__':
>         engine = create_engine('mysql://user:password@localhost/organisms')
>         Base.prepare(engine)


that form might be nice to add but currently you need the column to be present:

class Person(Base):
    __tablename__ = 'people'
    name = Column(String(20))
    __table_args__ = (PrimaryKeyConstraint(u'name'), {})




> 
> 
> When this runs, we get an error constructing the Person class (the __main__ 
> section is not hit).  This makes sense given that we haven't reflected the 
> table yet!
> 
> Traceback (most recent call last):
>   File "/home/ubuntu/deferred_reflection.py", line 10, in <module>
>     class Person(Base):
>   File 
> "/usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative/api.py", 
> line 53, in __init__
>     _as_declarative(cls, classname, cls.__dict__)
>   File 
> "/usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative/base.py", 
> line 251, in _as_declarative
>     **table_kw)
>   File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", 
> line 350, in __new__
>     table._init(name, metadata, *args, **kw)
>   File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", 
> line 427, in _init
>     self._init_items(*args)
>   File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", 
> line 70, in _init_items
>     item._set_parent_with_dispatch(self)
>   File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/base.py", line 
> 283, in _set_parent_with_dispatch
>     self._set_parent(parent)
>   File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", 
> line 2646, in _set_parent
>     super(PrimaryKeyConstraint, self)._set_parent(table)
>   File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", 
> line 2289, in _set_parent
>     ColumnCollectionMixin._set_parent(self, table)
>   File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py", 
> line 2257, in _set_parent
>     col = table.c[col]
>   File 
> "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/_collections.py", 
> line 156, in __getitem__
>     return self._data[key]
> KeyError: u'name'
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to