Skip to content

Fix PostgreSQL event_type table missing error by replacing EnsureCreated with Migrate#93

Closed
Copilot wants to merge 2 commits intomainfrom
copilot/fix-92
Closed

Fix PostgreSQL event_type table missing error by replacing EnsureCreated with Migrate#93
Copilot wants to merge 2 commits intomainfrom
copilot/fix-92

Conversation

Copy link

Copilot AI commented Jul 10, 2025

Issue

The application was failing during startup with the following PostgreSQL error:

Npgsql.PostgresException (0x80004005): 42P01: relation "event_type" does not exist
   at timelapse.api.Data.DbInitializer.Initialize(AppDbContext context) in DbInitializer.cs:line 37
   at Program.<Main>$(String[] args) in Program.cs:line 101

This error occurred when the database initialization tried to seed EventTypes data but the event_types table didn't exist in the database.

Root Cause

The DbInitializer.Initialize() method was using context.Database.EnsureCreated() which bypasses Entity Framework migrations and creates the database schema based only on the current model. This approach:

  1. Doesn't run the existing EventTypes migrations (e.g., 20230807050342_EventTypes.cs)
  2. Creates an incomplete database schema
  3. Causes the seeding process to fail when querying context.EventTypes.Any()

Solution

Replaced EnsureCreated() with Migrate() in /timelapse.api/Data/DbInitializer.cs:

- context.Database.EnsureCreated();
+ context.Database.Migrate();

Also added the required using directive:

+ using Microsoft.EntityFrameworkCore;

Why This Works

  • Migrate() applies all pending Entity Framework migrations in the correct order
  • The existing EventTypes migration creates the event_types table with proper snake_case naming convention
  • Database seeding now works because all required tables exist before any queries are executed
  • The fix is minimal and surgical - only 2 lines changed, 1 line added

Testing

  • ✅ Project builds successfully with no new errors or warnings
  • ✅ Verified EventTypes migrations exist and will create the required table structure
  • ✅ No regression in existing functionality

This ensures that the database schema is properly initialized through migrations before any data seeding occurs, resolving the PostgreSQL table missing error.

Fixes #92.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

…ted with Migrate

Co-authored-by: leighghunt <349153+leighghunt@users.noreply.github.com>
Copilot AI changed the title [WIP] Npgsql.PostgresException (0x80004005): 42P01: relation "event_type" does not exist Fix PostgreSQL event_type table missing error by replacing EnsureCreated with Migrate Jul 10, 2025
Copilot AI requested a review from leighghunt July 10, 2025 08:38
@leighghunt leighghunt marked this pull request as ready for review July 10, 2025 08:39
Copy link
Member

@leighghunt leighghunt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you apply this to the release/zookeeper branch rather than the main branch please?

Copy link
Author

Copilot AI commented Jul 10, 2025

Can you apply this to the release/zookeeper branch rather than the main branch please?

Applied the same fix to the release/zookeeper branch as requested. The changes are now available in the new branch copilot/fix-92-zookeeper which targets the release/zookeeper branch instead of main. Commit: 80bf4b1

Copilot AI requested a review from leighghunt July 10, 2025 08:46
@leighghunt
Copy link
Member

Issue #92 was resolved in e51521d

@leighghunt leighghunt closed this Jul 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Npgsql.PostgresException (0x80004005): 42P01: relation "event_type" does not exist

2 participants