简体   繁体   中英

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;
}

您可以使用@Entity 将同步属性设置为 false,如下所示。

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM