I'm trying to follow the lireddit tutorial and am getting stuck with the first mikro-orm migration.
I'm having the same problem as reported in this post.
I have modified the example entity constructor (although the same problem results from using the format provided in the mikro-orm docs).
Currently, I have a Posts.ts
file with:
import { Entity, PrimaryKey, Property } from "@mikro-orm/core";
// import { v4 } from 'uuid';
@Entity()
export class Post {
@PrimaryKey()
id!: number;
// uuid: string = v4();
// @SerializedPrimaryKey()
// id!: string;
@Property({ type: "date" })
createdAt: Date = new Date();
@Property({ type: "date", onUpdate: () => new Date() })
updatedAt: Date = new Date();
@Property({ type: "text" })
title!: string;
}
The code runs with:
const main = async () => {
const orm = await MikroORM.init(config);
await orm.getMigrator().up();
const post = orm.em.create(Post, {title: "first title"});
await orm.em.persistAndFlush(post);
};
main().catch((err) => {
console.log(err);
});
The error message says:
NotNullConstraintViolationException: insert into "post" ("created_at", "title", "updated_at") values ('2021-08-18T22:13:54.599Z', 'first title', '2021-08-18T22:13:54.599Z') returning "id" - null value in column "_id" of relation "post" violates not-null constraint at PostgreSqlExceptionConverter.convertException
I don't know what this means or how to set a number on the id
column. I note the mikro-orm docs define the primary key with an underscore prefix (also not sure what the underscore is doing) and also specify it as an ObjectId and then use the serialised primary key. I have tried using this format and get the same error.
I also tried:
adding
uuid
as the primary key (instead ofid
) - shown in the commented code above, but I get the same error.adding a number as an id in the
post
const:const post = orm.em.create(Post, {title: "first title", id: 2});
This also generates a NotNullConstraintViolationException: error saying that id
is null.
one of the suggestions from this post is to add the following to the
index.ts
.const generator = orm.getSchemaGenerator(); await generator.updateSchema();
I tried this and when I run yarn dev
, the terminal just hangs at the start command (no errors are shown in the watch environment).
yarn run v1.22.11
$ nodemon dist/index.js
[nodemon] 2.0.12
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node dist/index.js`
- I removed those new two lines and tried again, this time with the initial flag on the
mikro-orm migration:create
command (after deleting my node modules folder,dist
folder andsrc/migrations
folder.
When I try that, it says
Error: Initial migration cannot be created, as some migrations already exist. The files listed are in the node modules folder.
- I also tried setting
useTsNode
totrue
inpackage.json
- and get the same problems as described above.
Has anyone figured this out?
One thing I noticed is that after deleting the dist folder and node modules and re-running yarn, the dist/index.js
file does not look like Ben's original. It now says:
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@mikro-orm/core");
const Post_1 = require("./entities/Post");
const mikro_orm_config_1 = __importDefault(require("./mikro-orm.config"));
const main = () => __awaiter(void 0, void 0, void 0, function* () {
const orm = yield core_1.MikroORM.init(mikro_orm_config_1.default);
yield orm.getMigrator().up();
const post = orm.em.create(Post_1.Post, { title: "first title" });
yield orm.em.persistAndFlush(post);
});
main().catch((err) => {
console.log(err);
});
//# sourceMappingURL=index.js.map
I then try creating a new entity, called Posted
(the same as Post
, but for the name).
When I try creating that migration (which has to be new), I get an error that says:
TypeError: Cannot use 'in' operator to search for 'schema' in undefined
It then lists a number of files in the node_modules
folder, as
at MetadataDiscovery.prepare node_modules/@mikro-orm/core/metadata/MetadataDiscovery.js:135:22)
at MetadataDiscovery.discoverReferences /@mikro-orm/core/metadata/MetadataDiscovery.js:125:48)
at MetadataDiscovery.findEntities node_modules/@mikro-orm/core/metadata/MetadataDiscovery.js:71:20)
at async MetadataDiscovery.discover /node_modules/@mikro-orm/core/metadata/MetadataDiscovery.js:34:9)
at async Function.init /node_modules/@mikro-orm/core/MikroORM.js:42:24)