2

I am trying to refactor some models to a Joined Table Inheritance. I was able to get the configuration to work in our model library but haven't had any such success with our API which has similar models. The error I'm getting occurs the moment the Flask app attempts to interact with models in any form, not just with the inheritance tables.

Stacktrace

Traceback (most recent call last):
  File ".../lib/python3.5/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File ".../lib/python3.5/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File ".../lib/python3.5/site-packages/flask_restful/__init__.py", line 270, in error_router
    return original_handler(e)
  File ".../lib/python3.5/site-packages/flask_cors/extension.py", line 110, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File ".../lib/python3.5/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File ".../lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File ".../lib/python3.5/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File ".../lib/python3.5/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File ".../lib/python3.5/site-packages/flask_restful/__init__.py", line 270, in error_router
    return original_handler(e)
  File ".../lib/python3.5/site-packages/flask_cors/extension.py", line 110, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File ".../lib/python3.5/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File ".../lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File ".../lib/python3.5/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File ".../lib/python3.5/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File ".../app/views/views.py", line 177, in test
    vehicles = Vehicle.query.all()
  File ".../lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 452, in __get__
    mapper = orm.class_mapper(type)
  File ".../lib/python3.5/site-packages/sqlalchemy/orm/base.py", line 421, in class_mapper
    mapper = _inspect_mapped_class(class_, configure=configure)
  File ".../lib/python3.5/site-packages/sqlalchemy/orm/base.py", line 400, in _inspect_mapped_class
    mapper._configure_all()
  File ".../lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 1167, in _configure_all
    configure_mappers()
  File ".../lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 2688, in configure_mappers
    mapper._post_configure_properties()
  File ".../lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 1710, in _post_configure_properties
    prop.init()
  File ".../lib/python3.5/site-packages/sqlalchemy/orm/interfaces.py", line 183, in init
    self.do_init()
  File ".../lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1617, in do_init
    self._setup_join_conditions()
  File ".../lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1692, in _setup_join_conditions
    can_be_synced_fn=self._columns_are_mapped
  File ".../lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1960, in __init__
    self._determine_joins()
  File ".../lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 2043, in _determine_joins
    consider_as_foreign_keys=consider_as_foreign_keys
  File "<string>", line 2, in join_condition

  File ".../lib/python3.5/site-packages/sqlalchemy/sql/selectable.py", line 824, in _join_condition
    a, a_subset, b, consider_as_foreign_keys)
  File ".../lib/python3.5/site-packages/sqlalchemy/sql/selectable.py", line 881, in _joincond_scan_left_right
    if nrte.table_name == b.name:
AttributeError: 'Join' object has no attribute 'name'

Stacktrace 2

Traceback (most recent call last):
  File ".../lib/python3.5/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File ".../lib/python3.5/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File ".../lib/python3.5/site-packages/flask_restful/__init__.py", line 270, in error_router
    return original_handler(e)
  File ".../lib/python3.5/site-packages/flask_cors/extension.py", line 110, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File ".../lib/python3.5/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File ".../lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File ".../lib/python3.5/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File ".../lib/python3.5/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File ".../lib/python3.5/site-packages/flask_restful/__init__.py", line 270, in error_router
    return original_handler(e)
  File ".../lib/python3.5/site-packages/flask_cors/extension.py", line 110, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File ".../lib/python3.5/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File ".../lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File ".../lib/python3.5/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File ".../lib/python3.5/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File ".../app/views/views.py", line 177, in test
    vehicles = TrackingIncidentType.query.all()
  File ".../lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 452, in __get__
    mapper = orm.class_mapper(type)
  File ".../lib/python3.5/site-packages/sqlalchemy/orm/base.py", line 421, in class_mapper
    mapper = _inspect_mapped_class(class_, configure=configure)
  File ".../lib/python3.5/site-packages/sqlalchemy/orm/base.py", line 400, in _inspect_mapped_class
    mapper._configure_all()
  File ".../lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 1167, in _configure_all
    configure_mappers()
  File ".../lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 2685, in configure_mappers
    raise e
sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers.  Original exception was: 'Join' object has no attribute 'name'

Models

class Asset(db.Model):
    __tablename__ = 'assets'

    asset_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    asset_type_id = db.Column(db.ForeignKey('asset_types.asset_type_id'))
    ...

    __mapper_args__ = {
        'polymorphic_on': asset_type_id
    }


class Equipment(Asset):
    __tablename__ = 'equipment'

    asset_id = db.Column(db.ForeignKey('assets.asset_id'), primary_key=True)
    ...
    ...
    __mapper_args__ = {
        'polymorphic_identity':1
    }


class Vehicle(Asset):
    __tablename__ = 'vehicles'

    asset_id = db.Column(db.ForeignKey('assets.asset_id'), primary_key=True)
    ...
    ...
    __mapper_args__ = {
        'polymorphic_identity':2
    }

Asset Types

db=# select * from asset_types;
 asset_type_id |   name
---------------+-----------
             1 | equipment
             2 | vehicle
(2 rows)
2
  • @KurtRaschke I've added the second stacktrace I've had related to the error, but I'm confused by your question. Is the AttributeError in the first stacktrace not the thrown exception? Commented Feb 19, 2016 at 1:08
  • 1
    Sorry, there was a blank line towards the end of the stacktrace, and I thought that was the end—I didn't see the last few lines, including the AttributeError. My mistake. Commented Feb 19, 2016 at 1:13

1 Answer 1

1

Unfortunately I wasn't able to post all of the code related to this issue (thousands of lines of models), but I will attempt to explain the answer regardless...

I was refactoring two tables (Vehicles and Equipment) to a joined-inheritance hierarchy. Part of the refactor was dropping the vehicles.vehicle_id and equipment.equipment_id primary keys from the table. As such, I had to change all of the foreign key columns in my models (i.e. equipment_id = db.Column(db.ForeignKey('equipment.equipment_id'))). After breaking out the models into an isolated project I realised there was a model in an unexpected place in my application and it hadn't been refactored to the new FK. This caused the cryptic error.

So some code to hopefully clear this up:

Model before refactor

class TrackingEvent(db.Model):
    __tablename__ = 'tracking_events'

    tracking_event_id = db.Column(db.Integer, primary_key=True)
    equipment_id = db.Column(db.ForeignKey('equipment.equipment_id'))

Note: The referenced equipment.equipment_id no longer existed on the model OR in the DB.

Model after refactor

class TrackingEvent(db.Model):
    __tablename__ = 'tracking_events'

    tracking_event_id = db.Column(db.Integer, primary_key=True)
    asset_id = db.Column(db.ForeignKey('assets.asset_id'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.