0

I have the first Announcements model:

class Announcements(db.Model):
    __tablename__ = 'announcements'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(150), nullable=False)
    text = db.Column(db.Text(), nullable=False)
    category = db.Column(db.Integer(), nullable=False)
    duplicate = db.Column(db.Integer(), nullable=True)
    advert_id = db.Column(db.Integer(), nullable=True)
    rotate_photo = db.Column(db.Integer(), nullable=True)
    account = db.Column(db.Integer(), nullable=False)

And Account model:

class Accounts(db.Model):
    __tablename__ = 'accounts'
    id = Column(Integer, primary_key=True)
    api_key = db.Column(db.String(150), nullable=False)
    name = db.Column(db.String(150), nullable=True)
    status = db.Column(db.Integer(), nullable=True)

A need to set one to one relationship between these models Announcements and Account by bind: Announcements.account = Account.id

I tried this:

class Announcements(db.Model):
    __tablename__ = 'announcements'
    id = db.Column(db.Integer, primary_key=True)
    account = db.Column(db.Integer(), ForeignKey('account.id'), nullable=False)
    parent = db.relationship("Account", back_populates="child")


class Accounts(db.Model):
    __tablename__ = 'accounts'
    id = Column(Integer, primary_key=True)
    api_key = db.Column(db.String(150), nullable=False)
    name = db.Column(db.String(150), nullable=True)
    status = db.Column(db.Integer(), nullable=True)
    child = db.relationship("Announcements", uselist=False, back_populates="parent")

It gives me an error:

File "C:\Python27\lib\site-packages\sqlalchemy\orm\relationships.py", line 2100, in _determine_joins "specify a 'primaryjoin' expression." % self.prop) sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship Accounts.child - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.

Alice
  • 207
  • 2
  • 3
  • 10
  • It is not clear – Alice Dec 13 '17 at 18:34
  • Look my question again, I did some attempts – Alice Dec 13 '17 at 19:02
  • The error is very clear: "there are no foreign keys linking these tables". Your foreign key in the Announcements model references the id column of **account**, while the Accounts model's table is named **accounts**. As such this is closable as a simple typo. – Ilja Everilä Dec 13 '17 at 19:30

0 Answers0