LastModified metadata not mapped into JSONB column on document creation #4022
-
|
Hi, I used Marten’s StoreOptions(c =>
{
c.Schema.For<DocWithMeta>()
.Metadata(m => m.LastModified.MapTo(x => x.LastModified));
});However, while implementing create and update operations, I noticed an inconsistency in Marten’s behavior. The table below shows how the values are assigned in each column:
Is it expected that the JSONB data column is not automatically filled when creating the document? Below is a test that reproduces the issue. namespace DocumentDbTests.Metadata
{
public class document_metadata_specs : OneOffConfigurationsContext
{
[Fact]
public async Task doc_has_projected_data_after_storage()
{
StoreOptions(c =>
{
c.Schema.For<DocWithMeta>()
.Metadata(m => m.LastModified.MapTo(x => x.LastModified));
});
var doc = new DocWithMeta();
using (var session = theStore.LightweightSession())
{
session.Store(doc);
(await session.MetadataForAsync(doc)).ShouldBeNull();
await session.SaveChangesAsync();
}
using (var session = theStore.LightweightSession())
{
var loaded = await session.LoadAsync<DocWithMeta>(doc.Id);
loaded.LastModified.ShouldNotBe(DateTimeOffset.MinValue);
// I added the code below to reproduce the issue
// mt_last_modified works fine. The value is assigned when the document is created
var lastModifiedFromMetadataColumn = (await session
.QueryAsync<DateTimeOffset>("select mt_last_modified from document_metadata_specs.mt_doc_docwithmeta where id = ?", doc.Id))
.FirstOrDefault();
lastModifiedFromMetadataColumn.ShouldNotBe(DateTimeOffset.MinValue);
// LastModified property is not updated by Marten during document creation
var lastModifiedFromDataColumn = (await session
.QueryAsync<DateTimeOffset>("select (data->>'LastModified')::timestamptz from document_metadata_specs.mt_doc_docwithmeta where id = ?", doc.Id))
.FirstOrDefault();
lastModifiedFromDataColumn.ShouldNotBe(DateTimeOffset.MinValue);
// This will trigger TESTERROR
// Shouldly.ShouldAssertException : lastModifiedFromDataColumn
// should not be
// 1.01.0001 00:00:00 +00:00
// but was
}
}
// rest of document_metadata_specs |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Also note that once you Store (first creation), only a next fetch afresh will show the data populated for LastModified. |
Beta Was this translation helpful? Give feedback.
.Metadata(m => m.LastModified.MapTo(x => x.LastModified)-MapToautomatically pulls the value frommt_last_modifiedand update within your doc LastModified when data is fetched. Marten does not store the value within the serialized JSON in data. This is right behaviour and there is no issue as such.Also note that once you Store (first creation), only a next fetch afresh will show the data populated for LastModified.