Ranjan
Ranjan

Reputation: 328

table name specified more than once

I have the following tables configured:

class User(Base):
    __tablename__ = "user"

    id = Column(Integer, primary_key=True)
    name = Column(String)


class Gadget(Base):
    __tablename__ = "gadget"

    id = Column(Integer, primary_key=True)
    brand = Column(String)


class UserGadget(Base):
    __tablename__ = "user_gadget"

    user_id = Column(Integer, ForeignKey('user.id'), primary_key=True)
    gadget_id = Column(Integer, ForeignKey('gadget.id'), primary_key=True)

    user = relationship("User", backref=backref('userGadgets', order_by=user_id))
    gadget = relationship("Gadget", backref=backref('userGadgets', order_by=gadget_id))


class GadgetComponent(Base):
    __tablename__ = "gadget_component"

    id = Column(String, primary_key=True)
    gadget_id = Column(Integer,ForeignKey('gadget.id'))
    component_maker = Column(String)

    host = relationship("Gadget", backref=backref('components', order_by=id))


class ComponentUsingMetal(Base):
    __tablename__ = "component_metal"

    id = Column(Integer, primary_key=True)    
    component_id = Column(Integer, ForeignKey('GadgetComponent.id'))  
    metal = Column(String)

    component = relationship("GadgetComponent", backref=backref('gadgetComponentMetals', order_by=id))

On doing the following query: session.query(User).join("userGadgets", "gadget", "components","gadgetComponentMetals").filter(ComponentUsingMetal.metal == 'iron') , component_metal gets attached to the query twice giving an error 'table name component_metal specified more than once'.
Any idea what I am doing wrong?

Upvotes: 3

Views: 2272

Answers (1)

Ranjan
Ranjan

Reputation: 328

I traced the problem down to the following line in selectable.py: froms = [f for f in froms if f not in toremove] This line removes the tables which are already covered by joins so that FROM clause doesn't have the same table specified more than once. The line didn't remove component_metal even though toremove had it meaning that I had two different Table objects for the same db table. And then I noticed the import for component_metal's class, ComponentUsingMetal, looked different. The others imports looked like:

from myschema import GadgetComponent 
from myschema import Gadget
from python.myschema ComponentUsingMetal

As soon as fixed the import the issue went away.

Upvotes: 1

Related Questions