Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to map JSON properties: 42P01 - relation *** does not exist #1320

Closed
weitzhandler opened this issue Mar 23, 2020 · 4 comments
Closed

Comments

@weitzhandler
Copy link

Hi,

I'm setting up a mini model and I'm getting this exception when executing the code from the docs website.

Here's my code:

  public class SomeEntity
  {
    public int Id { get; set; }
    [Column(TypeName = "jsonb")]
    public Customer Customer { get; set; }
  }

  public class Customer    // Mapped to a JSON column in the table
  {
    public string Name { get; set; }
    public int Age { get; set; }
    public Order[] Orders { get; set; }
  }

  public class Order       // Part of the JSON column
  {
    public decimal Price { get; set; }
    public string ShippingAddress { get; set; }
  }
using (var dbContext = services.GetRequiredService<AppDbContext>())
{
  await dbContext.Database.MigrateAsync();

  dbContext.SomeEntities.Add(
    new SomeEntity
    {
      Customer = new Customer
      {
        Name = "Roji",
        Age = 35,
        Orders = new[]
        {
          new Order { Price = 3, ShippingAddress = "Somewhere" },
          new Order { Price = 3, ShippingAddress = "Nowhere" }
        }
      }
    });

  await dbContext.SaveChangesAsync();
}

When I call SaveChanges, I get the following exception:

Npgsql.PostgresException:
42P01: relation "SomeEntities" does not exist

Here's a repro project.

@roji
Copy link
Member

roji commented Mar 26, 2020

You're calling the MigrateAsync method, but your project doesn't have any actual migrations (those can be created with dotnet ef migrations add <name>. If you're just playing around, you likely want to call dbContext.Database.EnsureCreated instead, see this doc page.

Cross-posted as https://stackoverflow.com/questions/60856610/how-to-set-up-json-columns-with-npgsql

@roji roji closed this as completed Mar 26, 2020
@roji roji transferred this issue from npgsql/npgsql Mar 26, 2020
@weitzhandler
Copy link
Author

Thanks and sorry for bothering.
Posted comment on SO as well:

I can see it when using the Npgsql VSIX, if I query SELECT * FROM SomeEntities, I get that error:

image

But if I execute SELECT * FROM public."SomeEntities", I get this error:

image

But I do get the results thereafter (when using public."SomeEntities"):

image

@weitzhandler
Copy link
Author

weitzhandler commented Mar 27, 2020

roji transferred this issue from npgsql/npgsql yesterday
Oh, didn't realize the provider was an external repo, sorry.

Anyway, I keep on seeing this error in more occurrences in my project while migrating to Npgsql after calling MigrateAsync.

It helps if I substitute it with:

if (env.IsDevelopment())
  await appDbContext.Database.EnsureCreatedAsync();
else
  await appDbContext.Database.MigrateAsync();

But EF Core SQL provider respected the MigrateAsync call to create DB.

@roji
Copy link
Member

roji commented Mar 27, 2020

@weitzhandler unfortunately, VS is quite limited (and problematic) in its support of non-SQL Server databases. I looked into the identifier quoting problem with the VS query generator (with the help of a Microsoft dev who really knows it), and this is a real limitation in the product. There's nothing really we can do (see #1320 (comment)).

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

No branches or pull requests

2 participants