Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression

Ask Question

Please could someone help me with this error? I have actually been really struggling to find solid, simple examples for SQLAlchemy. Whilst there are plenty of Model examples of there is not much examples of how to use these Models.

The Error:

sqlalchemy.exc.NoForeignKeysError: 
Could not determine join condition between parent/child tables on relationship Species.sc_genus
- there are no foreign keys linking these tables.  
Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.

The Code

from sqlalchemy import Integer, Column, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relation
Base = declarative_base()
class Genus(Base):
    __tablename__ = 'genus'
    id = Column(Integer, primary_key=True)
    common_name = Column(String)
    scientific_name = Column(String)
    sc_sub_family = "sc_sub_family"
    def __repr__(self):
        return "<Genus(common_name='%s')>" % (self.scientific_name)
# Species is a child of Genus
class Species(Base):
    __tablename__ = 'species'
    id = Column(Integer, primary_key=True)
    common_name = Column(String)
    scientific_name = Column(String)
    sc_genus = relation("Genus", backref="species")
    def __repr__(self):
        return "<Species(common_name='%s')>" % (self.scientific_name)
def addSpecies(session):
    species = Species()
    species.common_name = "House Cat"
    species.scientific_name = "Felis catus"
    genus = Genus()
    genus.scientific_name = "Felis"
    session.add(genus)
    species.sc_genus = genus
    session.add(species)
    session.commit()
if __name__ == "__main__":
    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker
    ## A bunch of stuff to make the connection to the database work.
    engine = create_engine('sqlite:///foos.db', echo=True)
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()
    addSpecies(session)
    id = Column(Integer, primary_key=True)
    scientific_name = Column(String)
    # sc_sub_family = "sc_sub_family"
    def __repr__(self):
        return "<Genus(common_name='%s')>" % (self.scientific_name)
# Species is a child of Genus
class Species(Base):
    __tablename__ = 'species'
    id = Column(Integer, primary_key=True)
    common_name = Column(String)
    scientific_name = Column(String)
    sc_genus = relation("Genus", backref="species")
    sc_genus_id = Column(Integer, ForeignKey('genus.id'))
    def __repr__(self):
        return "<Species(common_name='%s')>" % (self.scientific_name)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.