question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update keys inside JSONB columns with migrations apply

See original GitHub issue

By default, changes to class properties do not affect the data inside [ColumnType(TypeName = "jsonb"]) entities resulting in a loose structure in the database and requiring manually invasive PgSQL queries to correct.

For example,

public class FooBar {
     [ColumnType(TypeName = "jsonb")]
     public Foo Foo { get; set; }
}
public class Foo {
     public string Bar { get; set; }
}

Updated with new properties…

public class Foo {
     public string Bar { get; set; }
     public int Baz { get; set; }
     public DateTimeOffset Qux { get; set; }
}

Results in a migration with changes that do not modify the keys inside jsonb columns (i.e. keys baz and qux are not automatically added to existing json data).

Expected Behaviour

  • Use postgres update table constructs to modify schema inside jsonb
  • Function similarly to traditional column insertion on migration apply when new properties are created

Is there an overriding or extension method call for performing this on dotnet ef database update?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sarcommented, Jan 23, 2021

As there is no DDL for json in postgres, the expectation is full data transformation DML statements in the migration to keep the data shape consistent with their code. (These migrations would be very write heavy, rewriting all rows/toasted values)

Well ideally it’d be more than just removal of course, but also change of type/shape from array to object and such things, again to actually be able to specify this in a model is already a ton of work 😃

I know it’s an academic discussion, but still 😃 What kind of DML would change existing JSON array values to object? I mean, even PostgreSQL doesn’t allow to change arbitrary column types - you have to specify a transformation yourself.

Great discussion as the performance implications and complexity of covering all possible use cases of transformations makes sense.

Think it’ll be easier for users to implement specific sql operations in the csharp project to validate consistency, apply transforms on jsonb data to align with type changes, relative to the code-first properties.

Thanks @roji and @NinoFloris for the explanation, closing this one out!

1reaction
rojicommented, Jan 23, 2021

I know it’s an academic discussion, but still 😃 What kind of DML would change existing JSON array values to object? I mean, even PostgreSQL doesn’t allow to change arbitrary column types - you have to specify a transformation yourself. In the same way, I can’t see what automatic transformations we’d be able to do… though of course users can always add a quick JSON update in their migrations via raw SQL, which applies the exact logic they want.

Read more comments on GitHub >

github_iconTop Results From Across the Web

update jsonb column from values in another table
I create a migration to to convert a jsonb column into a one-to-many table. -- upgrade insert into device_component ( warranty_request_uuid, ...
Read more >
Migrate existing data when altering the column type from ...
I want to migrate the existing string 'str1:str2' into the proper JSON string with keys as '{"key1":"str1","key2":"str2"}' before/while ...
Read more >
Modifying JSON data using JSON_MODIFY() in SQL Server
Suppose you define a variable in SQL Server and it holds JSON key-value pairs. We use JSON_MODIFY() function to update the JSON string....
Read more >
Work with JSONB data | Cloud Spanner
This page describes how to work with the JSONB data type when using Cloud Spanner. JSONB is a PostgreSQL data type used for...
Read more >
How do you properly index specific keys in a Postgres ...
How do you properly index specific keys in a Postgres JSONB column that's an array of hashes? Hi,. I have the following in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found