dimanche 13 septembre 2015

TypeError: an integer is required, SQLAlchemy + Bottlepy

I'm have the same problem as these questions.

TypeError: an integer is required, SQLAlchemy + Flask

Python - TypeError: an integer is required

SQLAlchemy version 1.0.8 and sqlite version 3.6.21

Both solutions say to check if the sqlite column is the same type as sqlalchemy is looking for, I've done that but am still getting the error. Here is my code.

# Create engine for SQLalchemy
engine = create_engine("sqlite:///db.db", echo=True)
Base = declarative_base(bind=engine)

class Loan(Base):
    __tablename__ = "loans"
    id = Column(Integer, primary_key=True)
    borrower = Column(String)
    given = Column(Integer)
    interest = Column(Integer)
    agreed_repay_date = Column(Date)
    repaid = Column(Integer)
    unpaid = Column(Boolean)
    original_thread = Column(String)
    given_date = Column(Date)
    paidback_date = Column(Date)
    info = Column(String)
    status = Column(String)

    def __init__(self, id, borrower=None, given=None, interest=None, agreed_repay_date=None, repaid=None, unpaid=False, original_thread=None, given_date=None, paidback_date=None, info=None, status="open"):
        self.id = id
        self.borrower = borrower
        self.given = given
        self.interest = interest
        self.agreed_repay_date = agreed_repay_date
        self.repaid = repaid
        self.unpaid = unpaid
        self.original_thread = original_thread
        self.given_date = given_date
        self.paidback_date = paidback_date
        self.info = info
        self.status = status

And here is the function that gets the loan.

# Gets loan based off ID provided
def getLoansID(self, id):
    for loan in s.query(Loan):
        if loan.id == id:
            return loan

# Route to get all loans from borrower
@route('/loans/id/<id:int>')
def viewLoan(id):
    loan_data = {
        'id': l.getLoansID(id).id,
        'borrower': l.getLoansID(idid).borrower,
        'given': l.getLoansID(id).given,
        'interest': l.getLoansID(id).interest,
        'agreed_repay_date': l.getLoansID(id).agreed_repay_date,
        'repaid': l.getLoansID(id).repaid,
        'unpaid': l.getLoansID(id).unpaid,
        'original_thread': l.getLoansID(id).original_thread,
        'given_date': l.getLoansID(id).given_date,
        'paidback_date': l.getLoansID(id).paidback_date,
        'info': l.getLoansID(id).info,
        'status': l.getLoansID(id).status,
        'color': color
        }
    return template('loan_info', **loan_data)

This is the error I get when running it.

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 862, in _handle
    return route.call(**args)
  File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 1732, in wrapper
    rv = callback(*a, **ka)
  File "index.py", line 194, in viewLoan
    'id': l.getLoansID(1).id,
  File "index.py", line 122, in getLoansID
    for loan in s.query(Loan):
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/loading.py", line 86, in instances
    util.raise_from_cause(err)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/loading.py", line 71, in instances
    rows = [proc(row) for row in fetch]
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/loading.py", line 428, in _instance
    loaded_instance, populate_existing, populators)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/loading.py", line 486, in _populate_full
    dict_[key] = getter(row)
TypeError: an integer is required

Again the ID column in the Loans table is in fact the integer type, and it also contains an integer. Thanks

Aucun commentaire:

Enregistrer un commentaire