SqlAlchemy issues with foreign keys
<p>I am getting the error </p>

<blockquote>
  <p>Could not parse rfc1738 URL from string 'MACHINE_IE'</p>
</blockquote>

<p>When I attempt to import the following</p>

<pre><code>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)
</code></pre>

<p>If I remove</p>

<blockquote>
  <p>metadata</p>
</blockquote>

<p>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</p>

<blockquote>
  <p>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'</p>
</blockquote>

<p>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.</p>

<p>Here is the connection data</p>

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

<p>Any assistance is appreciated.</p>

<p>You are using multiple instances of <code>Base</code>. You should be doing:</p>

<pre><code>Base = declarative_base()

class MACHINE(Base):
    ...

class IE(Base):
    ...

...
</code></pre>

python sqlalchemy