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

Code Generation in Quick SQL Creates Randomly Ordered Tables, Leading to Script Execution Failures #72

Open pallasinfotech opened 2 weeks ago

pallasinfotech commented 2 weeks ago

When generating SQL code in Quick SQL, the tool currently produces table creation statements in a the order it appears in the QuickSQL side order. This approach often results in dependency issues where a table that references another table is created before the referenced table, causing the script to fail upon execution.

It would be highly beneficial if Quick SQL could automatically analyze table dependencies and adjust the order of table creation accordingly. Specifically, the tool should:

Generate tables without dependencies first. Follow with tables that reference other tables, ensuring referenced tables are created prior to the dependent ones. This enhancement would ensure that even large models can be executed without manual intervention, improving efficiency and reliability.

Use Case: Quick SQL is an incredibly powerful tool that significantly reduces development time while ensuring the accuracy of triggers and packages. However, when working with medium to large models (e.g., 50 or 100 tables tables), the current behavior of generating SQL statements in a random order creates substantial overhead.

In my case, every time I regenerate SQL for my 62-table model, the table creation order changes. As a result, running the script fails unless I manually reorder the CREATE TABLE statements. This manual step negates some of the time-saving benefits Quick SQL is meant to provide.

Implementing dependency-aware script generation would eliminate this issue, allowing developers to focus more on design and less on troubleshooting script execution.

vadim-tropashko commented 2 weeks ago

Why tables have to be ordered? Here is the test:

 SQL> create table abc_emp (
  2      id             number generated by default on null as identity
  3                     constraint abc_emp_id_pk primary key,
  4      deptno         number,
  5      ename          varchar2(4000 char)
  6*  );

Table ABC_EMP created.

SQL> 
SQL> create index abc_emp_i1 on abc_emp (deptno);

Index ABC_EMP_I1 created.

SQL> 
SQL> create table abc_dept (
  2      id             number generated by default on null as identity
  3                     constraint abc_dept_id_pk primary key,
  4      dname          varchar2(4000 char)
  5* );

Table ABC_DEPT created.

SQL> 
SQL> 
SQL> alter table ABC_EMP add constraint  abc_emp_deptno_fk  foreign key (DEPTNO)  references ABC_DEPT;

Table ABC_EMP altered.