Vanderhoof / PyDBML

DBML parser and builder for Python
MIT License
101 stars 12 forks source link

Schema not being parsed in Table Group #20

Closed mjfii closed 2 years ago

mjfii commented 2 years ago

It appears that the new schema functionally does not work within the Table Group, where the schema is required to be explicit in the Table Group definition. Here is some valid dbml:


// this is a test scenario
Project test_scenario_1 {
  Note: '''
  test scenario for schema management
  '''
}

Table test.sales {
  sales_id int [primary key]
  calendar_date date [not null]
  quantity integer [not null]
  sales_amount decimal [not null]
  account_owner_employee_id int [not null]
  sales_rep_employee_id int [not null]
}

Table test.employee as emp {
  employee_id int [primary key]
  employee_name string [not null]
  manager_employee_id int [not null]
  country_code string [null]
  title_id string [null]
}

Table test.country as cnt {
  country_code string [primary key]
  country_name string [unique, not null]
}

Table test.title {
  title_id string [primary key]
  title_name string [not null]
}

Ref sales_refs_employee__account_owner {
  test.sales.account_owner_employee_id > test.employee.employee_id
}

Ref sales_refs_employee__sales_rep {
  test.sales.sales_rep_employee_id > test.employee.employee_id
}

Ref employee_refs_employee__manager {
  test.employee.manager_employee_id > test.employee.employee_id
}

Ref employee_refs_country {
  test.employee.country_code > test.country.country_code
}

Ref employee_refs_title__current {
  test.employee.title_id > test.title.title_id
}

Table test.calendar {
  calendar_date date [not null]
  fiscal_month int [not null]
}

Ref sales_refs_calendar {
  test.sales.calendar_date > test.calendar.calendar_date
}

Tablegroup test_table_group_1 {
  test.sales
  test.employee
  test.country
  test.title
  test.calendar
}

It will yield an error due to pydbml/definitions/table_group.py, I believe.

  File "~git~/venv/lib/python3.8/site-packages/pydbml/parser/parser.py", line 175, in locate_table
  raise TableNotFoundError(f'Table {full_name} not present in the database')
pydbml.exceptions.TableNotFoundError: Table ['test', 'sales'].test not present in the database

If we remove the Table Group, the schema in the below parses normally.

// this is a test scenario
Project test_scenario_1 {
  Note: '''
  test scenario for schema management
  '''
}

Table test.sales {
  sales_id int [primary key]
  calendar_date date [not null]
  quantity integer [not null]
  sales_amount decimal [not null]
  account_owner_employee_id int [not null]
  sales_rep_employee_id int [not null]
}

Table test.employee as emp {
  employee_id int [primary key]
  employee_name string [not null]
  manager_employee_id int [not null]
  country_code string [null]
  title_id string [null]
}

Table test.country as cnt {
  country_code string [primary key]
  country_name string [unique, not null]
}

Table test.title {
  title_id string [primary key]
  title_name string [not null]
}

Ref sales_refs_employee__account_owner {
  test.sales.account_owner_employee_id > test.employee.employee_id
}

Ref sales_refs_employee__sales_rep {
  test.sales.sales_rep_employee_id > test.employee.employee_id
}

Ref employee_refs_employee__manager {
  test.employee.manager_employee_id > test.employee.employee_id
}

Ref employee_refs_country {
  test.employee.country_code > test.country.country_code
}

Ref employee_refs_title__current {
  test.employee.title_id > test.title.title_id
}

Table test.calendar {
  calendar_date date [not null]
  fiscal_month int [not null]
}

Ref sales_refs_calendar {
  test.sales.calendar_date > test.calendar.calendar_date
}
Vanderhoof commented 2 years ago

Thanks for reporting! Fixed in 1.0.2 (I'll try to release it today or next weekend)

Vanderhoof commented 2 years ago

(Probably better to keep it open until the actual release)