I am writing a basic banking app to keep my skills fresh and am trying to implement a transaction table into the mix to of course keep track of transactions. This happens as I am trying to insert a new transaction programmatically using Python and psycopg[binary]. I have it the same as I do for creating new accounts and customers so I am at a loss for words after trying everything I can think of including using a formatted string for my parameters and the same problem was occurring. I will include a clip of the code that has the unexpected behavior as well as clips where the code is similar and follows the expected behavior. I even forced an error in the good code so that it would show that it has the data in a single tuple.
def create_transaction(self, transaction: Transaction) -> Transaction:
logging.info("Beginning DAL function create transaction")
sql = "insert into banking.transactions values (default, %s, %s, %s, %s) returning transaction_id;"
cursor = connection.cursor()
cursor.execute(sql, (transaction.account_id, transaction.transaction_type, transaction.amount, transaction.time_and_date))
connection.commit()
transaction_id = cursor.fetchone()[0]
transaction.transaction_id = transaction_id
logging.info("Finishing DAL function create transaction")
return transaction
def create_customer(self, customer: Customer) -> Customer:
logging.info("Beginning DAL function create customer")
sql = "insert into banking.customers values (default, %s, %s, %s, %s) returning customer_id;"
cursor = connection.cursor()
cursor.execute(sql, (customer.first_name, customer.last_name, customer.username, customer.password))
connection.commit()
customer_id = cursor.fetchone()[0]
customer.customer_id = customer_id
logging.info("Finishing DAL function create customer")
return customer
def create_account(self, account: BankAccount) -> BankAccount:
logging.info("Beginning DAL function create account")
sql = "insert into banking.bank_accounts values (default, %s, %s) returning account_id;"
cursor = connection.cursor()
cursor.execute(sql, (account.customer_id, account.balance))
connection.commit()
account_id = cursor.fetchone()[0]
account.account_id = account_id
logging.info("Finishing DAL function create account")
return account
query = 'insert into banking.bank_accounts values (default, %d, %s) returning account_id;' params = (-1, 50.0)
query = 'insert into banking.transactions values (default, %s, %s, %s, %s) returning transaction_id;'
params = ((-1,), ('deposit',), (50.0,), '2022-11-28 21:56:22.000486')
def execute(
self: _Self,
query: Query,
params: Optional[Params] = None,
*,
prepare: Optional[bool] = None,
binary: Optional[bool] = None,
) -> _Self:
"""
Execute a query or command to the database.
"""
try:
with self._conn.lock:
self._conn.wait(
self._execute_gen(query, params, prepare=prepare, binary=binary)
)
except e.Error as ex:
raise ex.with_traceback(None)
E psycopg.errors.InvalidTextRepresentation: invalid input syntax for integer: "(-1)"
I have it the same as I do for creating new accounts and customers so I am at a loss for words after trying everything I can think of including using a formatted string for my parameters and the same problem was occurring. I have tried typecasting it to an integer before using the execute method but it happens within that line of code.