Mar-29-2022, 05:44 PM
Hi!
Please help me with the error below.
I also tried with the command: user_id = Column (String (13), ForeignKey ('users.user_id')) without child and parent as I used below.
Please help me with the error below.
I also tried with the command: user_id = Column (String (13), ForeignKey ('users.user_id')) without child and parent as I used below.
Error:
raise exc.NoReferencedTableError(
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'userstransactions.transaction_id' could not find table 'users' with which to generate a foreign key to target column 'user_id'
repository -> userstransactions.pyfrom sqlalchemy.orm import sessionmaker from Model.Domain.users_transactions import UsersTransactions from Utils.utils import Base, engine class DBUsersTransactionsRepository: def __init__(self): self.session = sessionmaker(engine)() # CREATE def add_transactions(self, transaction_id, user_id, currency, amount, vendor, date_time): new_transaction = UsersTransactions( transaction_id=transaction_id, user_id=user_id, currency=currency, amount=amount, vendor=vendor, date_time=date_time ) self.session.add(new_transaction) self.session.commit() if __name__ == '__main__': Base.metadata.create_all(engine) depo_repo = DBUsersTransactionsRepository() depo_repo.add_transactions( transaction_id=123, user_id='1234', currency='EUR', vendor='Altex', date_time='2022-01-02' ) depo_repo.add_transactions( transaction_id=124, user_id='1235', currency='EUR', vendor='EMAG', date_time='2022-04-02' ) depo_repo.add_transactions( transaction_id=125, user_id='1234', currency='USD', vendor='EMAG', date_time='2022-03-02' )domain -> userstransaction.py
from sqlalchemy import Column, String, Integer, Float, ForeignKey from sqlalchemy.types import DateTime from Utils.utils import Base class UsersTransactions(Base): __tablename__ = 'userstransactions' transaction_id = Column(Integer, ForeignKey('users.user_id'), primary_key=True) user_id = Column(String(13)) currency = Column(String(3), ForeignKey('currencies.currency', ondelete='CASCADE')) amount = Column(Float(2), nullable=False) vendor = Column(String(100), nullable=False) date_time = Column(DateTime, nullable=False) parent_id = Column(String, ForeignKey('users.user_id')) def __repr__(self): return f'{self.transactions_id}, {self.user_id}, {self.currency}, {self.amount}, {self.vendor}, {self.date_time}'domain -> users.py
from sqlalchemy import Column, String from sqlalchemy.orm import relationship from sqlalchemy.types import Date, DateTime from Utils.utils import Base class Users(Base): __tablename__ = 'users' user_id = Column(String(13), primary_key=True) first_name = Column(String(30), nullable=False) last_name = Column(String(30), nullable=False) email_name = Column(String(50), nullable=False) address = Column(String(50), nullable=False) phone_number = Column(String(10), nullable=False) date_of_birth = Column(Date, nullable=False) join_date = Column(DateTime, nullable=False) children = relationship('UsersTransactions') def __repr__(self): return f'user id = {self.user_id}\n' \ f'first name = {self.first_name}\n' \ f'last name = {self.last_name}\n' \ f'email = {self.email_name}\n' \ f'phone = {self.phone_number}\n' \ f'date of birth = {self.date_of_birth}\n' \ f'join date = {self.join_date}'