Issue with Editor Mjoin and additional fields.

Issue with Editor Mjoin and additional fields.

djagercddjagercd Posts: 13Questions: 4Answers: 0
edited January 2019 in Free community support

Hi all, I am using the datatables.net-editor-server.
I am trying to use the mjoin with additional fields for created_at and updated_at.

const editor = new Editor(db, 'users', 'uuid')
        .debug(true)
        .fields(
            new Field('users.uuid')
                .set(Field.SetType.Create)
                .setValue(uuidv4()),
            new Field('users.name')
                .validator(Validate.notEmpty())
                .validator(Validate.dbUnique()),
            new Field('users.email')
                .validator(Validate.notEmpty())
                .validator(Validate.dbUnique())
                .validator(Validate.email()),
            new Field('users.password')
                .validator(Validate.notEmpty()),
            new Field("users.created_at")
                .set(Field.SetType.Create)
                .setValue(new Date(Date.now())),
            new Field("users.updated_at")
                .set(Field.SetType.Edit)
                .setValue(new Date(Date.now())),
            new Field("users.deleted_at")
        )
        .join(
            new Mjoin('roles')
                .link('users.uuid', 'user_roles.user_uuid')
                .link('roles.uuid', 'user_roles.role_uuid')
                .fields(
                    new Field('uuid')
                        .validator(Validate.required())
                        .options(
                            new Options().table('roles').value('uuid').label('name')
                        ),
                    new Field('name'),
                    new Field('created_at')
                        .set(Field.SetType.Create)
                        .setValue(new Date(Date.now())),
                    new Field('updated_at')
                        .set(Field.SetType.Edit)
                        .setValue(new Date(Date.now()))
                )
        );

When I update a field from the client it gives me an UnhandledPromiseRejectionWarning on the created_at column.

I have debugged the Knex code and what it Editor does is delete on the user_roles table before doing an insert.

```

{ method: 'del',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ 'de67a42c-4d1e-402e-b4ec-d9842de59ee8' ],
__knexQueryUid: 'c1ddae59-aa29-4665-b47f-69b791a8197d',
sql: 'delete from "user_roles" where ("user_uuid" = ?)',
returning: undefined }

{ method: 'insert',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings:
[ 'e83416fb-d82c-4236-9508-f756e4798193',
'de67a42c-4d1e-402e-b4ec-d9842de59ee8' ],
__knexQueryUid: 'f2043af6-6fb3-4453-9514-f3ab4eefb5b4',
sql:
'insert into "user_roles" ("role_uuid", "user_uuid") values (?, ?)',
returning: undefined }

The returned error is:

UnhandledPromiseRejectionWarning: error: null value in column "created_at" violates not-null constraint

This is due to me having a not null constraint on the created_at and updated_at fields.
I know I could set these as default on the database level, but the table is being generated by Sequelize (model is used for other purposes).

Any idea's on how to fix?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 62,211Questions: 1Answers: 10,205 Site admin
    Answer ✓

    Hi,

    So the problem here is that when using Mjoin with a link table is that it doesn't actually write to the Mjoined table (roles in this case), but rather it will write to the link table (user_roles) - and that only a simple reference to the two tables the link is being created for.

    If you have a created_at property in the link table (I'm not clear that you do), you'd need to use a database default or trigger in the case of the update.

    Regards,
    Allan

This discussion has been closed.