Elli Zorro
Elli Zorro

Reputation: 501

How I can use exist table in mysql using typeorm entity?

Using typeorm, I connect to the mysql db. Several tables already exist in the database. I try to describe a table using the entity module, but my definition overwrites an existing table. How with entity I can fully inherit from an existing table?

import "reflect-metadata";
import { Entity, PrimaryGeneratedColumn, Column, PrimaryColumn, OneToMany } from "typeorm";

@Entity("devices")
export class Devices {
    @PrimaryGeneratedColumn()
    ID: number;

    @Column({ type: "varchar", length: 255 })
    Name: string;

    @Column({ type: "int", nullable: false })
    DevID: number;

    @Column({ type: "int", nullable: false })
    Longtitude: number;

    @Column({ type: "int", nullable: false })
    Latitude: number;

    @PrimaryColumn({ type: "varchar", length: 255 })
    URL_VWS: string;
}

Upvotes: 8

Views: 11032

Answers (2)

senuraa
senuraa

Reputation: 173

You could set the the synchronize property to false with @Entity like below.

@Entity({name:"devices", synchronize: false})

Upvotes: 11

jctaoo
jctaoo

Reputation: 99

In ormconfig.json

change

{
  "type": "mysql",
  "host": "localhost",
  "port": 3306,
  "database": "demo",
  "username": "root",
  "entities": ["src/**/**.entity{.ts,.js}"],
  "synchronize": true
}

to

{
  "type": "mysql",
  "host": "localhost",
  "port": 3306,
  "database": "demo",
  "username": "root",
  "entities": ["src/**/**.entity{.ts,.js}"],
  "synchronize": false
}

Upvotes: 3

Related Questions