I am using SQL Alchemy v(0.9.1) which has the automap functionality. This allows me to create classes and relationships automatically. http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/automap.html
The problem I am experiencing is that when using automap_base, I see that not all the tables that are available in the metadata.tables list are mapped.
There are no errors when preparing, except that I am unable to access the class (e.g. Base.classes.Example_Table) from the Base after calling automap_base()
from sqlalchemy import create_engine, MetaData from sqlalchemy.orm import create_session  from sqlalchemy.ext.automap import automap_base  engine = create_engine("mysql+mysqldb://")  Base = automap_base() Base.prepare(engine, reflect = True)  session = create_session(bind = engine)   Then I run to find that classes do not exist for all tables in metadata (I did not use only = [] argument in metadata)
for mappedclass in Base.classes:     print mappedclass  for mdtable in metadata.tables:     print mdtable   Only to find that Example_Table (with the following create syntax) does not have a class
    CREATE TABLE `Example_Table` (   `id` bigint(20) NOT NULL AUTO_INCREMENT,   `attributeType` varchar(255) NOT NULL,   PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3290719 DEFAULT CHARSET=latin1   i.e. Base.class.Example_Table returns the following error
    --------------------------------------------------------------------------- AttributeError                            Traceback (most recent call last) <ipython-input-15-94492ae1b8ba> in <module>()       7 type(metadata.tables)       8  ----> 9 Base.classes.Example_Table  /usr/local/lib/python2.7/site-packages/sqlalchemy/util/_collections.pyc in __getattr__(self, key)     172             return self._data[key]     173         except KeyError: --> 174             raise AttributeError(key)     175      176     def __contains__(self, key):  AttributeError: Example_Table   I don't think this problem happens because my Example_Table name has an underscore in it.
I think the problem is related to the fact that my Example_Table does not have a primary key. The Example_Table is only meant to link two other tables.
The SQLAlchemy ORM, in order to map to a particular table, needs there to be at least one column denoted as a primary key column; multiple-column, i.e. composite, primary keys are of course entirely feasible as well.
Define an extension to the sqlalchemy. ext. declarative system which automatically generates mapped classes and relationships from a database schema, typically though not necessarily one which is reflected.
SQLAlchemy schema metadata is a comprehensive system of describing and inspecting database schemas. The core of SQLAlchemy's query and object mapping operations is supported by database metadata.
A base class stores a catlog of classes and mapped tables in the Declarative system. This is called as the declarative base class. There will be usually just one instance of this base in a commonly imported module. The declarative_base() function is used to create base class. This function is defined in sqlalchemy.
Figured it out by combing through the reference/reframing the problem.
In case it helps someone else -
Because SQLAlchemy ORM is based on an identity map model, one cannot map (or automap) a table that does not have a primary key. An arbitrary primary key should be specified.
http://docs.sqlalchemy.org/en/latest/faq/ormconfiguration.html#how-do-i-map-a-table-that-has-no-primary-key
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With