On Sep 25, 2011, at 9:21 PM, Devraj wrote: > Hi all, > > SQLAlchemy 0.7.1 / Python 2.7.1 on Ubuntu 11.04 server / Apache 2.2.x > + mod_wsgi 3.3.x > > I am *very* new to SQLAlchemy, so please be kind. We are trying to use > SQLAlchemy with an existing database / tables. Our preference is to > use the Metadata auto discover mechanism so we don't have to define > each column name in the mapper classes (not opposed to writing out the > column names if that's what we have to do, just being lazy). > > Consider the following definition of a class that maps to a table with > two columns, section_id (Integer, PK), section_name (String), > section_number (String) > > Base = declarative_base() > Base.metadata.reflect(bind=db.engine) > > class Section(Base): > __table__ = Base.metadata.tables['section'] > > > all works well interacting with the database, we can insert, retrieve > records and of course refer to the columns as attributes of the Python > object. > > We are writing code where we need to dynamically discover the primary > key of the table / object. I have Googled around to find that objects > should have a primary_key attribute that should give me what I am > after, but all the documentation I've found so far refer to 0.5 and > this obviously doesn't exists in 0.7 > > My question is, is there an equivalent of the primary_key property > which we can use to auto discover the primary key of an object.
I'm mystified that google results are still returning the 0.5 documentation as I've had /docs/05/ under "disallow" in robots.txt for nearly a year now. Not sure what terms you googled for, I just tried "sqlalchemy primary key table" - the first two hits are from the 0.7 documentation, and the first is the "schema" documentation which is the correct result. The primary key of a Table object in the form of an iterable of Column objects can be reached via the "primary_key" attribute, documented in the example here: http://www.sqlalchemy.org/docs/core/schema.html#accessing-tables-and-columns The Table from a mapped class can be reached via class_mapper(cls) or if declarative is in use the __table__ attribute as well. hope this helps. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.