Skip to content

LastModified metadata not mapped into JSONB column on document creation #3959

@jacek13

Description

@jacek13

Hi,
I recently had to configure metadata for new documents in one of our production applications that has been under development for quite some time.

I used Marten’s Metadata.MapTo configuration like this:

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:

mt_last_modified data->>'LastModified' (JSONB)
Create Assigned value Unassigned value
Update Assigned value Assigned value

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 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions