在过去的三天里,我会发疯,因为我无法再将具有@OneToMany()关系的新记录添加到MySQL数据库。

OneToMany实体如下:

import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { Detail } from './Detail';


@Entity()
export class Biergarten {

    @PrimaryGeneratedColumn()
    id: number;

    @Column( { default: true } )
    isBiergarten: boolean;

    @Column("simple-array")
    attendees: string[];

    @Column( { unique: true } )
    date: number;

    @Column( { unique: true } )
    link: number;

    @Column("simple-json")
    location: { latitude: number, longitude: number };

    @Column()
    name: string;

    @OneToMany( type => Detail, detail => detail.biergarten, { eager: true, cascade: true } )
    details: Detail[];

    @Column( { type: "mediumtext", nullable: false } )
    geojson: string;

    @Column( { type: "char", length: 32 } )
    hash: string;

}


ManyToOne-Side如下:

import {Entity, Column, ManyToOne, PrimaryGeneratedColumn} from "typeorm";
import { Biergarten } from './Biergarten';


@Entity()
export class Detail {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    comment: string;

    @Column()
    picture: string;

    @ManyToOne( type => Biergarten, biergarten => biergarten.details, { onDelete: "CASCADE" } )
    biergarten: Biergarten;

}


我尝试了一堆与“级联”等的组合,但是每次尝试使用以下代码添加新记录时,都会收到一条MySQL错误消息:

const repository = getRepository( Biergarten );
const biergarten = repository.create( {
  attendees: _.body.attendees,
  date: _.body.date,
  link: _.body.link,
  isBiergarten: _.body.isBiergarten,
  location: _.body.location,
  name: _.body.name,
  geojson: '',
  details: [],
  hash: '',
});

let details: Array<Detail> = Array.from(_.body.details);
details.forEach( detail => biergarten.details.push( detail )
await repository.save( biergarten );
);


错误:

r] query: START TRANSACTION
[server] query: INSERT INTO `biergarten`(`id`, `isBiergarten`, `attendees`, `date`, `link`, `location`, `name`, `geojson`, `hash`) VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?) -- PARAMETERS: [1,"Robert",1563989940,2084,"{\"latitude\":48.12694671104566,\"longitude\":11.644134521484377}","testtestest","",""]
[server] query: SELECT `Biergarten`.`id` AS `Biergarten_id`, `Biergarten`.`isBiergarten` AS `Biergarten_isBiergarten` FROM `biergarten` `Biergarten` WHERE `Biergarten`.`id` = ? -- PARAMETERS: [204]
[server] query: INSERT INTO `detail`(`id`, `comment`, `picture`, `biergartenId`) VALUES (DEFAULT, ?, ?, ?) -- PARAMETERS: [" :think: ",{},204]
[server] query failed: INSERT INTO `detail`(`id`, `comment`, `picture`, `biergartenId`) VALUES (DEFAULT, ?, ?, ?) -- PARAMETERS: [" :think: ",{},204]
[server] error: { Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' 204)' at line 1
[server]     at Packet.asError (/home/robert/Repository/BiergartenByBike/server/node_modules/mysql2/lib/packets/packet.js:684:17)
[server]     at Query.execute (/home/robert/Repository/BiergartenByBike/server/node_modules/mysql2/lib/commands/command.js:28:26)
[server]     at PoolConnection.handlePacket (/home/robert/Repository/BiergartenByBike/server/node_modules/mysql2/lib/connection.js:449:32)
[server]     at PacketParser.Connection.packetParser.p [as onPacket] (/home/robert/Repository/BiergartenByBike/server/node_modules/mysql2/lib/connection.js:72:12)
[server]     at PacketParser.executeStart (/home/robert/Repository/BiergartenByBike/server/node_modules/mysql2/lib/packet_parser.js:75:16)
[server]     at Socket.Connection.stream.on.data (/home/robert/Repository/BiergartenByBike/server/node_modules/mysql2/lib/connection.js:79:25)
[server]     at Socket.emit (events.js:198:13)
[server]     at addChunk (_stream_readable.js:288:12)
[server]     at readableAddChunk (_stream_readable.js:269:11)
[server]     at Socket.Readable.push (_stream_readable.js:224:10)
[server]   code: 'ER_PARSE_ERROR',
[server]   errno: 1064,
[server]   sqlState: '42000',
[server]   sqlMessage:
[server]    'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near \' 204)\' at line 1' }
[server] query: ROLLBACK


我还建立了一个最小的git仓库来查找错误的位置,但是它运行良好:https://github.com/sagerio/ChildInsertFailed

我不知道现在要看哪里,也没有看到原始存储库和最小存储库中使用的表没有区别。

谢谢,
罗伯特

最佳答案

我知道了,问题出在字段Detail.picture上,该字段被定义为字符串,但来自上载为File,因此查询中的语法错误。

关于mysql - 无法添加具有OneToMany关系的新记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57188920/

10-09 21:56