automap_base fails on my database

Issue #3449 resolved
yoch created an issue

Hi,

I use 1.0.5 version, but this bug occurs also with 0.9.8.

Here a simple test-case to reproduce the bug :

CREATE TABLE `hardware` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `hdw_type` varchar(15) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `hdw_type` (`hdw_type`),
  CONSTRAINT `hardware_ibfk_1` FOREIGN KEY (`hdw_type`) 
    REFERENCES `hdw_type` (`type`) 
    ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE TABLE IF NOT EXISTS `hdw_type` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `type` VARCHAR(15) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `hdw_type_UNIQUE` (`type` ASC)
);

This simple code snippet fail :

Base = automap_base()
Base.prepare(engine, reflect=True)

Traceback :

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/automap.py", line 795, in prepare
    map_config.map()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative/base.py", line 593, in map
    return super(_DeferredMapperConfig, self).map()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative/base.py", line 530, in map
    **self.mapper_args
  File "<string>", line 2, in mapper
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 627, in __init__
    self._configure_properties()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 1318, in _configure_properties
    setparent=True)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 1525, in _configure_property
    prop = self._property_from_column(key, prop)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 1693, in _property_from_column
    (key, self, column.key, prop))
sqlalchemy.exc.ArgumentError: WARNING: when configuring property 'hdw_type' on Mapper|hardware|hardware, column 'hdw_type' conflicts with property '<RelationshipProperty at 0xfc0bb0; hdw_type>'. 
To resolve this, map the column to the class under a different name in the 'properties' dictionary.  Or, to remove all awareness of the column entirely (including its availability as a foreign key), use the 'include_properties' or 'exclude_properties' mapper arguments to control specifically which table columns get mapped.

I think this is related to my columns/indexes naming.

Thank you

Comments (4)

  1. Mike Bayer repo owner

    no bug here, please read http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#overriding-naming-schemes

    example:

    from sqlalchemy import create_engine
    
    e = create_engine("mysql://scott:tiger@localhost/test", echo=True)
    e.execute("""
    
    CREATE TABLE IF NOT EXISTS `hdw_type` (
      `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
      `type` VARCHAR(15) NOT NULL,
      PRIMARY KEY (`id`),
      UNIQUE INDEX `hdw_type_UNIQUE` (`type` ASC)
    ) engine=InnoDB""")
    
    e.execute("""
    CREATE TABLE IF NOT EXISTS `hardware` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `hdw_type` varchar(15) NOT NULL,
      PRIMARY KEY (`id`),
      KEY `hdw_type` (`hdw_type`),
      CONSTRAINT `hardware_ibfk_1` FOREIGN KEY (`hdw_type`)
        REFERENCES `hdw_type` (`type`)
        ON DELETE CASCADE ON UPDATE CASCADE
    ) engine=InnoDB
    """)
    
    
    def name_for_scalar_relationship(base, local_cls, referred_cls, constraint):
        name = referred_cls.__name__.lower() + "_ref"
        return name
    
    from sqlalchemy.ext.automap import automap_base
    Base = automap_base()
    Base.prepare(
        e, reflect=True,
        name_for_scalar_relationship=name_for_scalar_relationship
    )
    
    print(Base.classes.hardware.hdw_type_ref)
    
  2. Log in to comment