Why not set the relationship between tables via ForeignKey?

from sqlalchemy import create_engine
from sqlalchemy.ext.DRI import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.the orm import sessionmaker, relationship

engine = create_engine('sqlite:///:memory:', echo=True)
Session = sessionmaker(bind=engine)
sess = Session()
Base = declarative_base()

class User(Base):
 __tablename__ = 'users'

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

class Address(Base):
 __tablename__ = 'addresses'
 id = Column(Integer, primary_key=True)
 email = Column(String, nullable=False)
 user_id = relationship('User', ForeignKey('Users.id'))

Base.metadata.create_all(engine)


Error:
AttributeError: 'ForeignKey' object has no attribute 'foreign_keys'
June 7th 19 at 14:57
2 answers
June 7th 19 at 14:59
Solution
something like this:

class Address(Base):
 __tablename__ = 'addresses'
 id = Column(Integer, primary_key=True)
 email = Column(String, nullable=False)
 user_id = Column(Integer, ForeignKey('users.id'))
 user = relationship(User)
June 7th 19 at 15:01
class Friend(Base):
 __tablename__ = 'friend'

 user_id = Column(Integer, ForeignKey(User.id), primary_key=True)
 friend_id = Column(Integer, ForeignKey(User.id), primary_key=True)
 request_status = Column(Boolean)

 user = relationship('User', foreign_keys='Friend.user_id')
 friend = relationship('User', foreign_keys='Friend.friend_id')

As I understand it ,is that requests are written in Flask , can only help it to tear the piece of code

Find more questions by tags PythonSQLAlchemy