Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions src/packages/database/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1166,20 +1166,6 @@ class Model {
const run = async (trx: Object) => {
const { hooks, logger, primaryKey } = this;
const instance = Reflect.construct(this, [props, false]);
let statements = [];

const associations = Object
.keys(props)
.filter(key => (
Boolean(this.relationshipFor(key))
));

if (associations.length) {
statements = associations.reduce((arr, key) => [
...arr,
...updateRelationship(instance, key, trx)
], []);
}

await runHooks(instance, trx, hooks.beforeValidation);

Expand All @@ -1191,12 +1177,28 @@ class Model {
hooks.beforeSave
);

const runner = createRunner(logger, statements);
const runner = createRunner(logger, []);
const [[primaryKeyValue]] = await runner(await create(instance, trx));

Reflect.set(instance, primaryKey, primaryKeyValue);
Reflect.set(instance.rawColumnData, primaryKey, primaryKeyValue);

let statements = [];
const associations = Object
.keys(props)
.filter(key => (
Boolean(this.relationshipFor(key))
));

if (associations.length) {
statements = associations.reduce((arr, key) => [
...arr,
...updateRelationship(instance, key, trx)
], []);
}

await Promise.all(statements);

Reflect.defineProperty(instance, 'initialized', {
value: true,
writable: false,
Expand Down