xnuinside / simple-ddl-parser

Simple DDL Parser to parse SQL (HQL, TSQL, AWS Redshift, BigQuery, Snowflake and other dialects) ddl files to json/python dict with full information about columns: types, defaults, primary keys, etc. & table properties, types, domains, etc.
MIT License
179 stars 40 forks source link

FK column from referencing table not present in "references" but present in "alter" #196

Open tduval-unifylogic opened 1 year ago

tduval-unifylogic commented 1 year ago

Describe the bug when a foreign key is defined within CREATE TABLE tablename ():

CONSTRAINT fk_order FOREIGN KEY (order_identifier) REFERENCES orders(order_id),

The column order_identifier is not output in the results, so no way to know which column from the table is the referencing column. e.g., output:

"references": [
  {
    "table": "orders",
    "columns": [
      "order_id"
    ],
    "schema": null,
    "on_delete": null,
    "on_update": null,
    "deferrable_initially": null,
    "constraint_name": "fk_order"
  }
]

To Reproduce Steps to reproduce the behavior (this example is using postgres as dialect):

  1. process file1.sql and view results and find alter in order_items table results. Column "name": "order_identifier" is present
    sql = open('file1.sql', 'r').read()
    results = parse_from_file(sql, group_by_type=True)
    print(json.dumps(results, indent=2))
  2. process file2.sql and find constraints/references in order_items table results. Column "name": "order_identifier" is not present
    sql = open('file2.sql', 'r').read()
    results = parse_from_file(sql, group_by_type=True)
    print(json.dumps(results, indent=2))

Expected behavior to see similar results output from alter, where the name of referencing column is presented:

"alter": {
  "columns": [
    {
        "name": "order_identifier",
        "constraint_name": "fk_order",
        "references": {
          "table": "orders",
          "schema": null,
          "on_delete": null,
          "on_update": null,
          "deferrable_initially": null,
          "column": "product_id"
            }
    }, ...

Screenshots n/a

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context Add any other context about the problem here.

tduval-unifylogic commented 1 year ago

@xnuinside , checking in... would really like to use your parser as it is amazing! The referencing column metadata missing is a blocker for us though. Any idea on when this could get resolved?

xnuinside commented 1 year ago

@tduval-unifylogic hi! I will have time on this weekends, I hope, I will create new release with fix

tduval-unifylogic commented 1 year ago

Thank you! Access to parsed comments would also be a blessing! I know there's an issue captured on this already. We're creating ontologies from schemas, so any semantic context we can get is a bonus.

xnuinside commented 1 year ago

sure, I will try to add it

cfhowes commented 8 months ago

I am having the same problem. Will try and post a PR in the next couple of days.

cfhowes commented 8 months ago

I don't have the ability to update ticket status, but I believe that as of version 0.32.0 this should be solved.