Search code examples
oracleoracle-apexauto-increment

How can i implement a auto Increment pk in a interative grid


I tried to implement an auto increment on my default interactive grid page by following several tutorials on YouTube, but each tutorial taught differently and none solved my problem, the new line is filled in but filled in as "t1000" and I would like it to start with just a number starting from 1 the interactive grid sql commands table

I tried to auto increment using a trigger or a javascript script


Solution

  • There is no such thing as an "autoincrement on an interactive grid". If there is an interactive grid on a table, you can automatically generate the new pk for any new rows in the table. I'm assuming that is what you mean.

    Here is a full example:

    create table, load some sample data

    Note that the table has the primary key column defined as an identity column, which is the preferred way to generate primary key columns. This will take care of the automatic generation of the new value

    create table mytable (
        id       number generated by default on null as identity
                 constraint mytable_id_pk primary key,
        item     varchar2(100 char),
        price    number
    );
    
    -- load data
    
    insert into mytable ( id, item, price ) values ( 1, 'table', 200 );
    insert into mytable ( id, item, price ) values ( 2, 'chair', 150 );
    insert into mytable ( id, item, price ) values ( 3, 'sofa', 300 );
    
    commit;
    
    alter table mytable
    modify id generated always  as identity restart start with 4;
    
    create editable interactive grid on mytable

    Set "Primary Key" and "Query Only" to yes for column id. Set column type for id to "Display Only".

    enter image description here

    Run the page and click "Add Row". The id column will be empty for the new row. That is expected. At this moment the row does not exist yet in the table, but only in your browser session. When you click Save, the rows will be added in the table and the id column will be populated with the value.

    If you set the column to "Text Field" instead of "Display Only", then this gives you the option to manually enter the primary key (which is discouraged since the id column is an identity column). In this case the IG will get a temporary primary key value of t1000 when you click "Add Row". Notice that you'll get t1001,t1002,... if you add additional rows. The IG model uses this to enforce uniqueness on the client data before submit. As long as the row is not saved it's not known what the primary key value in the table will be. Once you submit, the columns with a "txxxx" value will get the generated value from the table.