question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to define ObjectID type column (other than _id)

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [x] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

I have an entity :

@Entity()
export class RolePermission implements IRolePermission {
    @ObjectIdColumn() id?: ObjectID;

    @Column()
    @IsNotEmpty()
    roleId: ObjectID;

    @Column()
    @IsNotEmpty()
    permissionId: ObjectID;
}

The code compiles. But when I start my server, this error throws :

> node ./dist/server.js

error:  ColumnTypeUndefinedError: Column type for RolePermission#roleId is not defined and cannot be guessed. Make sure you have turned on an "emitDecoratorMetadata": true option in tsconfig.json. Also make sure you have imported "reflect-metadata" on top of the main entry file in your application (before any entity imported).If you are using JavaScript instead of TypeScript you must explicitly provide a column type.

I have turned on the option in my tsconfig.json and I import reflect-metadata at the top of my file server.ts.

I read the code about the decorator ObjectIdColumn and if I think it used only for primary use case, so it’s not applicable for my need.

Thanks a lot for your help

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

15reactions
pleerockcommented, Jun 5, 2018

you need to install mognodb typings and import ObjectID from mongodb, like this:

import {ObjectID} from "mongodb";

@Entity()
export class RolePermission implements IRolePermission {
    @ObjectIdColumn() id?: ObjectID;

    @Column()
    @IsNotEmpty()
    roleId: ObjectID;

    @Column()
    @IsNotEmpty()
    permissionId: ObjectID;
}

I know this is confusing, and its something we have to resolve.

6reactions
Alex0007commented, Sep 1, 2018

Having two ObjectID from various places is pain

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set ObjectId as a data type in mongoose - Stack Overflow
After setting a virtual property, I tried: db.cats.find({categoryId: ObjectId("the id")}) but got null results. when I use db.cats.find({_id ...
Read more >
Fundamentals of ObjectID fields—Help | ArcGIS Desktop
In ArcMap or ArcCatalog, you can open the table's Properties dialog box and click the Fields tab. If there is no field listed...
Read more >
ObjectId — MongoDB Manual
Generate a New ObjectId. To generate a new ObjectId, use ObjectId() with no argument: x = ObjectId() ; Specify a Hexadecimal String. To...
Read more >
What Is MongoDB's _id Field and How to Use It - Orange Matter
As MongoDB documentation explains, "ObjectIds are small, likely unique, fast to generate, and ordered." The _id field is a 12-byte Field of BSON ......
Read more >
Fields - ODMantic
The ObjectId data type is the default primary key type used by MongoDB. An ObjectId comes with many information embedded into it (timestamp,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found