Server Script - How to create a connection between Sales invoice and Payment Entry?

Hi, I have created a Server Script that automatically creates a Sales Invoice and a Payment Entry after a Sales order is submitted, but the problem is that my payment entry is not getting linked to my Sales Invoice, how could I connect them?

This is my code:

# Create a Payment Entry
payment_entry = frappe.get_doc({
    "doctype": "Payment Entry",
    "party_type": party_type,
    "party": sales_invoice.customer,
    "payment_type": "Receive",
    "posting_date": frappe.utils.today(),
    "reference_date": frappe.utils.today(),
    "reference_no": reference_no,
    "company": sales_invoice.company,
    "paid_to": payment_account,
    "paid_from": paid_from,
    "paid_received": sales_invoice.grand_total,
    "paid_amount": sales_invoice.grand_total,
    "received_amount": sales_invoice.grand_total,
    "total_amount": sales_invoice.grand_total,
    "mode_of_payment": mode_of_payment,
    "target_exchange_rate": "MXN"
})

# add reference to Sales Invoice
payment_entry.append("references", {
"reference_doctype": "Sales Invoice",
"reference_name": sales_invoice.name,
"total_amount": sales_invoice.grand_total
})

# Insert payment entry
payment_entry.insert()
    
# Save and Submit Payment Entry
payment_entry.save()
payment_entry.submit()

Thanks for your help!

Thanks to @ landaverdelbo I could solve this, the only thing I was missing was the "“allocated_amount” on the append functions, so now it looks like this:

    # add reference to Sales Invoice
    payment_entry.append("references", {
        "reference_doctype": "Sales Invoice",
        "reference_name": sales_invoice.name,
        "due_date": frappe.utils.today(),
        "allocated_amount": sales_invoice.grand_total
    })