Dennis_M
Dennis_M

Reputation: 360

SqlAlchemy issues with foreign keys

I am getting the error

Could not parse rfc1738 URL from string 'MACHINE_IE'

When I attempt to import the following

class MACHINE(declarative_base()):
    __tablename__ = 'MACHINE'
    MACHINE_UID = Column(Integer, primary_key=True)
    MACHINE_IP = Column(String)
    MACHINE_NAME = Column(String)
    MACHINE_INFO = Column(String)

class IE(declarative_base()):
    __tablename__ = 'IE'
    IE_UID = Column(Integer, primary_key=True)
    IE_VERSION = Column(String)
    IE_MAJOR = Column(String)

class MACHINE_IE(declarative_base().metadata):
    __tablename__ = 'MACHINE_IE'
    IE_UID = Column(Integer, ForeignKey("IE.IE_UID"), primary_key=True)
    MACHINE_UID = Column(Integer, ForeignKey("MACHINE.MACHINE_UID"))
    EFFECTIVE_DATE = Column(DateTime)

If I remove

metadata

then I can import the module and perform a query on the "MACHINE" and "IE" tables and display the data from the rows. However when I query "MACHINE_IE" and then try to display some data from the query I get the following

sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'MACHINE_IE.IE_UID' could not find table 'IE' with which to generate a foreign key to target column 'IE_UID'

I am guessing I need "metadata" in there but the error seems to be complaining about the db connection string when I use it. However I am able to connect to the db w/o issue if "metadata" is removed.

Here is the connection data

connect_string = "oracle+cx_oracle://{0}:{1}@{2}:{3}/{4}".format(user, pw, server, port, sid)

Any assistance is appreciated.

Upvotes: 0

Views: 199

Answers (1)

univerio
univerio

Reputation: 20508

You are using multiple instances of Base. You should be doing:

Base = declarative_base()

class MACHINE(Base):
    ...

class IE(Base):
    ...

...

Upvotes: 2

Related Questions