Skip to content

Save does not return id column back (id is undefined) #2422

@secret-agent-B

Description

@secret-agent-B

Issue type:
[x] bug report

Database system/driver:
[x] postgres

TypeORM version:
[x] latest

I am working on a RESTful API and I am kind of following the example on the documentation; however - I could not get tyoeorm to return the id from the database. As you can see from the result at the bottom of this issue, the id column is not even there.

Model looks like this;

import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, ManyToMany, JoinColumn, JoinTable, RelationId} from "typeorm";
import {Personnel} from "./personnel";
import {Status} from "./status";
import {IssueCategory} from "./issueCategory";
import {Project} from "./project";
import {IssueComment} from "./issueComment";


@Entity("issue",{schema:"public"})
export class Issue {

    @Column("integer",{
        generated:true,
        nullable:false,
        primary:true,
        name:"id"
        })
    id:number;


    @Column("character varying",{
        nullable:false,
        length:250,
        name:"title"
        })
    title:string;


    @Column("text",{
        nullable:false,
        name:"additionalInfo"
        })
    additionalInfo:string;


    @Column("text",{
        nullable:true,
        name:"technicalInfo"
        })
    technicalInfo:string | null;



    @ManyToOne(type=>Personnel, Personnel=>Personnel.issues,{  nullable:false, })
    @JoinColumn({ name:'submitterId'})
    submitter:Personnel | null;



    @ManyToOne(type=>Status, Status=>Status.issues,{  nullable:false, })
    @JoinColumn({ name:'statusId'})
    status:Status | null;



    @ManyToOne(type=>IssueCategory, IssueCategory=>IssueCategory.issues,{  nullable:false, })
    @JoinColumn({ name:'categoryId'})
    category:IssueCategory | null;


    @Column("date",{
        nullable:false,
        name:"dueDate"
        })
    dueDate:string;


    @Column("timestamp with time zone",{
        nullable:false,
        name:"createdOn"
        })
    createdOn:Date;


    @Column("timestamp with time zone",{
        nullable:true,
        name:"updatedOn"
        })
    updatedOn:Date | null;



    @ManyToOne(type=>Project, Project=>Project.issues,{  nullable:false, })
    @JoinColumn({ name:'projectId'})
    project:Project | null;



    @OneToMany(type=>IssueComment, IssueComment=>IssueComment.reference)
    issueComments:IssueComment[];
}

NodeJs code;

import { Router, Request, Response, NextFunction } from 'express';
import { Issue } from '../dal/models/issue';
import { getManager } from 'typeorm';
import { Project } from '../dal/models/project';
import { Personnel } from '../dal/models/personnel';
import { Status } from '../dal/models/status';
import { IssueCategory } from '../dal/models/issueCategory';

add = (req: Request, res: Response, next: NextFunction) => {
    try {
      let issueRepo = getManager().getRepository(Issue);

      let input = new Issue();
      input.title = req.body.title;
      input.additionalInfo = req.body.additionalInfo;
      input.technicalInfo = req.body.technicalInfo;
      input.dueDate = req.body.dueDate;
      input.createdOn = new Date();

      input.project = new Project();
      input.project.id = req.body.projectId;

      input.submitter = new Personnel();
      input.submitter.id = req.body.submitterId;

      input.status = new Status();
      input.status.id = req.body.statusId;

      input.category = new IssueCategory();
      input.category.id = req.body.categoryId;

      console.log(input);

      issueRepo
        .save(input)
        .then(issue => {
          console.log('Issue has been saved. Issue id is:', issue.id);
          res.send({
            message: 'Success',
            status: 201,
            issue
          });
        });
    } catch (error) {
      res.send({
        message: 'Failed',
        status: 500,
        error
      });
    }
  }

Result;

{
  title: 'Sample Issue',
  additionalInfo: 'User details here...',
  technicalInfo: 'Stack trace here...',
  dueDate: '6/26/2018',
  createdOn: 2018-06-27T16:39:37.567Z,
  project: Project { id: 1 },
  submitter: Personnel { id: 1 },
  status: Status { id: 1 },
  category: IssueCategory { id: 1 },
  updatedOn: null 
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions