django-hordak icon indicating copy to clipboard operation
django-hordak copied to clipboard

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

Open kbhalerao opened this issue 4 years ago • 4 comments

To replicate:

  • Start a fresh Django-Hordak project with Django 3+ and Postgres 12 - bug may be related to PG version
  • Create a new Account, results in
django.db.utils.ProgrammingError: cannot cast type bigint to boolean
LINE 1: SELECT NEW.parent_id::BOOL
                            ^
QUERY:  SELECT NEW.parent_id::BOOL
CONTEXT:  PL/pgSQL function check_account_type() line 3 at IF

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;

kbhalerao avatar Aug 09 '21 19:08 kbhalerao

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)

BuildWithLal avatar Jun 26 '22 15:06 BuildWithLal

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()",
        )
    ]

BuildWithLal avatar Jul 04 '22 20:07 BuildWithLal

@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 avatar Sep 17 '22 08:09 PetrDlouhy

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

PetrDlouhy avatar Sep 18 '22 08:09 PetrDlouhy