SQL autocomplete object columns doesn't show

Hello. When I’m using it without nested objects, it works well. It shows columns.

const oldConfig: SQLConfig = {
   defaultSchema: 'old_schema',
   defaultTable: 'old_table',
   schema: { 'old_schema.old_table': ['old_col_1', 'old_col_2', 'old_col_3'] },
};

But when I test it with nested objects, it doesn’t show columns.

const oldConfig: SQLConfig = {
  defaultSchema: "new_schema",
  defaultTable: "new_table",
  schema: {
    new_schema: {
      self: {
        type: "db",
        label: "new_schema"
      },
      children: {
        new_table: {
          self: {
            type: "table",
            label: "new_table"
          },
          children: [
            {
              type: "column",
              label: "new_col_1"
            },
            {
              type: "column",
              label: "new_col_2"
            },
            {
              type: "column",
              label: "new_col_3"
            }
          ]
        }
      }
    }
  }
};

This patch should fix this.

I have 4 level nested object.
Catalog > Database > Table > Column

I that case I couldn’t see top columns if I set defaultSchema and defaultTable.

Please see sandbox

Yes, defaultSchema refers to the top level of the hierarchy, and defaultTable to the 2nd level. I’m not quite sure how to unify those with a 4 level nesting.

Hi @marijn , there seems to have been introduced a bug with that patch: Make the way the completion schema is defined more regular · codemirror/lang-sql@5f9ea3b · GitHub

Could you show an example of a configuration that triggers that crash?

My usage is like

sql({
  schema: { tableName: [ {label: 'field', detail: 'Explanation', type: 'property'} ] },
  defaultTable: 'tableName',
  upperCaseKeywords: true,
}),

Now type anotherTable. into the editor and it triggers the error “TypeError: Cannot read properties of null (reading ‘children’)”.

Looking at the code, I think it is quite obvious that the code is not correct. We set level = defaultSchema in this line even though defaultSchema can be null. Then in the next iteration we read level.children even though level can be null. Curious that this isn’t caught by Typescript.

Thanks. That helped reproduce it. This patch should help.

2 Likes