oracle / quicksql

A library for generating DDL SQL and entity-relationship-diagrams from Quick SQL code
Universal Permissive License v1.0
53 stars 11 forks source link

"Prefix primary keys with table name" with SYSGUID error #45

Closed simonhunt8475 closed 9 months ago

simonhunt8475 commented 10 months ago

When using the setting "Prefix primary keys with a sysguid population method, then a trigger is created that uses the :new.id rather than :new.[table name]_id. This causes an error when the script is run.

Populating the primary key in the trigger is not actually needed anyway as there is a default value on the column in the table.

Screenshot 2024-01-27 at 22 09 24
vadim-tropashko commented 10 months ago

Can't reproduce. https://krisrice.io/quick-sql-standalone.html running version 1.1.4

person
        person_name
# settings = { PK: "GUID" } 

outputs

-- create tables

create table person (
    id             number default on null to_number(sys_guid(), 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') 
                   constraint person_id_pk primary key,
    person_name    varchar2(4000 char)
);

-- triggers
create or replace trigger person_biu
    before insert or update
    on person
    for each row
begin
    if :new.id is null then
        :new.id := to_number(sys_guid(), 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
    end if;
end person_biu;
/

In soon to be released version 1.2.1, the trigger is nuked.

simonhunt8475 commented 9 months ago

You need to use the setting # settings = { PK: "GUID", "prefixpkwithtname": "Y" } then you'll see that the trigger doesnt include the prefix table name. If you're nuking the trigger, then that will do the job, thanks.

vadim-tropashko commented 9 months ago

1.2.1 is out, and you can test it here. The trigger code for GUID is not generated.

simonhunt8475 commented 9 months ago

Hi, this is not working if you use the setting api:Y. The API's created use a column ID that is not prefixed with the table name, which then errors. I will raise a new issue.

vadim-tropashko commented 9 months ago

Test case:

person
        person_name
# settings = { PK: "GUID", api:y, "prefixpkwithtname": "Y" } 

Output:

...
-- APIs
create or replace package person_api
is

    procedure get_row (
        p_id        in  number,
--------^^^
        p_person_name   out  varchar2
    );
...
vadim-tropashko commented 9 months ago

Closing again: duplicate of 57