sqlmesh icon indicating copy to clipboard operation
sqlmesh copied to clipboard

Model is missing but sqlmesh plan/run is successful

Open wahmedswl opened this issue 7 months ago • 10 comments

Hi!

We have model patient_management.v_patient_insurance in sqlmesh but it's not created. Plan or Run, doesn't re-create etc and there is no error as well.

We can delete and then re-add it and it will be created but don't know why SQLMesh is not able to identify it.

SQL Error [42P01]: ERROR: relation "patient_management.v_patient_insurance" does not exist
  Position: 15

Error position: line: 1740 pos: 14
dotenvx run -- sqlmesh run --skip-janitor --ignore-cron --select-model="patient_management.v_patient_insurance"
⟐ injected env (1) from .env · [email protected]
[v1.0.0] Copy to blob: Object=dbo.vm_SaviPatientInsurance;Blob=;Container=reports-savi
[v1.0.0] Updating fields of table: Name="postgres"."sqlmesh__data_pipeline"."data_pipeline__ext_pt_ins__2065245981"
[v1.0.0] Analyzing table: Name="postgres"."sqlmesh__data_pipeline"."data_pipeline__ext_pt_ins__2065245981"
[1/1] data_pipeline.ext_pt_ins                   [full refresh]                             7.04s   
[1/1] data_pipeline.external_patient_insurance   [recreate view]                            2.92s   
[1/1] patient_management.v_patient_insurance     [recreate view]                            1.46s   
Executing model batches ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 3/3 • 0:00:26                                                                                                                   
                                                                                                                                                                                                          
✔ Model batches executed

Run finished for environment 'prod'

Thanks

wahmedswl avatar Apr 14 '26 11:04 wahmedswl

Could you elaborate? You defined a model, ran a plan, but it didn't get created?

yuki-kakegawa avatar May 30 '26 05:05 yuki-kakegawa

Hey @wahmedswl - still facing this issue?

mday-io avatar Jun 12 '26 14:06 mday-io

@mday-io I have deleted and re-created; it was Kind = View;

wahmedswl avatar Jun 18 '26 14:06 wahmedswl

Could you elaborate? You defined a model, ran a plan, but it didn't get created?

@StuffbyYuki If sqlmesh run command is used; it wouldn't have executed if sqlmesh plan wasn't executed before, isn't it?

wahmedswl avatar Jun 18 '26 15:06 wahmedswl

@wahmedswl, is patient_management.v_patient_insurance a Python model?

lafirm avatar Jun 24 '26 03:06 lafirm

SQL model, kind = view

wahmedswl avatar Jun 24 '26 05:06 wahmedswl

Thanks @wahmedswl but can you share an MRE please? I don't see the behavior you described in SQL models

lafirm avatar Jun 24 '26 13:06 lafirm

@lafirm Sorry, i don't have that; because i re-created the view and it fixed it. You can look at the above execution; sqlmesh run is completed and showing the exact model is ran but when ran query on that; it got failed.

wahmedswl avatar Jun 28 '26 09:06 wahmedswl

It again happened; what kind of logs you require?

SQL Error [42P01]: ERROR: relation "patient_management.v_patient" does not exist Position: 15

[1/1] patient_management.v_patient [recreate view] 3.36s
Executing model batches ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 1/1 • 0:00:11

✔ Model batches executed

Run finished for environment 'prod'

2026-08-06 23:59:01,187 - ThreadPoolExecutor-0_0 - sqlmesh.core.snapshot.evaluator - INFO - Listing data objects in schema postgres.sqlmesh__patient_management (evaluator.py:1635) 2026-08-06 23:59:02,781 - ThreadPoolExecutor-0_0 - sqlmesh.core.engine_adapter.base - INFO - Executing SQL: SELECT current_catalog (base.py:2654) 2026-08-06 23:59:08,787 - ThreadPoolExecutor-1_0 - sqlmesh.core.snapshot.evaluator - INFO - Evaluating snapshot SnapshotId<"postgres"."patient_management"."v_patient": 3058037368> (evaluator.py:730) 2026-08-06 23:59:10,399 - ThreadPoolExecutor-1_0 - sqlmesh.core.snapshot.evaluator - INFO - Inserting data for snapshot SnapshotId<"postgres"."patient_management"."v_patient": 3058037368> (evaluator.py:971) 2026-08-06 23:59:10,480 - ThreadPoolExecutor-1_0 - sqlmesh.core.snapshot.evaluator - INFO - Inserting batch (2026-08-06 16:00:00, 2026-08-06 18:58:57.394000) into postgres.sqlmesh__patient_management.patient_management__v_patient__1394140191' (evaluator.py:1003) 2026-08-06 23:59:10,480 - ThreadPoolExecutor-1_0 - sqlmesh.core.snapshot.evaluator - INFO - Replacing view 'postgres.sqlmesh__patient_management.patient_management__v_patient__1394140191' (evaluator.py:2754) 2026-08-06 23:59:10,499 - ThreadPoolExecutor-1_0 - sqlmesh.core.engine_adapter.base - INFO - Executing SQL: CREATE VIEW "sqlmesh__patient_management"."patient_management__v_patient__1394140191" ("account_no", "chart_no", "medical_record_no", "first_name", "last_name", "middle_initial", "full_name", "date_of_birth", "age", "gender", "email", "address_line1", "address_line2", "city", "state", "zip_code", "full_address", "home_tel_no", "work_tel_no", "cell_no", "all_phone_no", "pm_system", "search_id", "search_text", "external_practice_id", "practice_name", "practice_description", "external_silo_id", "silo_name", "active", "created_at", "updated_at", "practice_npi") AS SELECT "ep"."account_no" AS "account_no", "ep"."chart_no" AS "chart_no", "ep"."medical_record_no" AS "medical_record_no", "ep"."first_name" AS "first_name", "ep"."last_name" AS "last_name", "ep"."middle_initial" AS "middle_initial", "ep"."full_name" AS "full_name", "ep"."date_of_birth" AS "date_of_birth", "ep"."age" AS "age", "ep"."gender" AS "gender", "ep"."email" AS "email", "ep"."address_line1" AS "address_line1", "ep"."address_line2" AS "address_line2", "ep"."city" AS "city", "ep"."state" AS "state", "ep"."zip_code" AS "zip_code", "ep"."full_address" AS "full_address", "ep"."home_tel_no" AS "home_tel_no", "ep"."work_tel_no" AS "work_tel_no", "ep"."cell_no" AS "cell_no", "ep"."all_phone_no" AS "all_phone_no", "ep"."pm_system" AS "pm_system", "ep"."search_id" AS "search_id", "ep"."search_text" AS "search_text", "ep"."practice_id" AS "external_practice_id", "ep"."practice_name" AS "practice_name", "vp"."description" AS "practice_description", "ep"."silo_id" AS "external_silo_id", "ep"."silo_name" AS "silo_name", "ep"."active" AS "active", "ep"."inserted_at" AS "created_at", "ep"."refreshed_at" AS "updated_at", "vp"."npi" AS "practice_npi" FROM "postgres"."sqlmesh__data_pipeline"."data_pipeline__external_patient__4278315867" AS "ep" JOIN "postgres"."sqlmesh__profile_management"."profile_management__v_practice__4233316499" AS "vp" ON "ep"."practice_id" = "vp"."external_practice_id" (base.py:2654) 2026-08-06 23:59:11,665 - ThreadPoolExecutor-1_0 - sqlmesh.core.state_sync.db.facade - INFO - Adding interval (2026-08-06 16:00:00, 2026-08-06 18:58:57.394000) for snapshot SnapshotId<"postgres"."patient_management"."v_patient": 3058037368> (facade.py:639) 2026-08-06 23:59:11,665 - ThreadPoolExecutor-1_0 - sqlmesh.core.state_sync.db.interval - INFO - Pushing intervals for snapshot SnapshotId<"postgres"."patient_management"."v_patient": 3058037368> (interval.py:215) 2026-08-06 23:59:12,251 - MainThread - root - INFO - Shutting down the event dispatcher (dispatcher.py:159)

Thanks

wahmedswl avatar Aug 06 '26 19:08 wahmedswl

It again happened; what kind of logs you require?

The logs aren't particularly helpful. It would be useful to have an MRE, could you also share the SQLMesh version you're using? That may help narrow down the issue.

I also have a theory about what might be happening. When sqlmesh plan runs, SQLMesh creates a model snapshot based on the model's SQL and then creates the corresponding physical and virtual layers. If someone or something manually drops a view, for example "patient_management.v_patient_insurance", neither sqlmesh plan nor sqlmesh run will recreate it because the SQL file still exists and the snapshot fingerprint remains unchanged. As far as SQLMesh is concerned, there have been no changes to the model, even though the underlying view was manually deleted.

Interestingly, if you remove the "patient_management.v_patient_insurance" model file, run sqlmesh plan, then add the file back and run sqlmesh plan again, SQLMesh will recreate the view because it detects a change in the model state.

Given that, I'd recommend checking whether there's a cleanup process, deployment script or manual action that may have deleted the view. It would also be worth confirming whether anyone recently performed maintenance or schema cleanup activities.

lafirm avatar Aug 11 '26 12:08 lafirm