adamcharnock / django-hordak

Double entry accounting in Django
http://django-hordak.readthedocs.io
MIT License
231 stars 55 forks source link

Create New Account fails with error in `check_account_type()` #57

Closed kbhalerao closed 1 month ago

kbhalerao commented 2 years ago

To replicate:

Workaround: Change the trigger code to do an intermediate cast from BigInt to Int, as follows. Might fail in weird ways I don't understand. Probably needs to be set up in the migration

BEGIN
    IF NEW.parent_id::INT::BOOL THEN
        NEW.type = (SELECT type FROM hordak_account WHERE id = NEW.parent_id);
    END IF;
    RETURN NEW;
END;
lalatgithub commented 2 years ago

i am getting this same issue in a fresh django project

python = 3.10
Django = 4.0.5
psycopg2 = 2.9.3
django-hordak = 1.10.2

postgres (PostgreSQL) 14.2 (Debian 14.2-1.pgdg110+1)
lalatgithub commented 2 years ago

Adding a new migration fixed it for me. By replacing

IF NEW.parent_id::BOOL THEN with IF NEW.parent_id::INT::BOOL THEN

from django.db import migrations

class Migration(migrations.Migration):
    dependencies = [("hordak", "0029_alter_leg_amount_currency_and_more")]

    operations = [
        migrations.RunSQL(
            """
            CREATE OR REPLACE FUNCTION check_account_type()
                RETURNS TRIGGER AS
            $$
            BEGIN
                IF NEW.parent_id::INT::BOOL THEN
                    NEW.type = (SELECT type FROM hordak_account WHERE id = NEW.parent_id);
                END IF;
                RETURN NEW;
            END;
            $$
            LANGUAGE plpgsql;
            """,
            "DROP FUNCTION check_account_type()",
        )
    ]
PetrDlouhy commented 1 year ago

@kbhalerao @lalatgithub I would like to release fix for this, but I would need to be able to replicate. It works fine on my project as well as in tests all using PostgreSQL.

Do you use some settings that might be responsible for this such as:

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

?

PetrDlouhy commented 1 year ago

Yeah. I can confirm, that this problem is specific to using BigAutoField. I will try to make the fix migration.

adamcharnock commented 1 month ago

it looks like this was fixed in migration 0032_check_account_type_big_int.py. I'll close this