Skip to content

Manager insert query with auto-generated columns #3837

@clovis1122

Description

@clovis1122

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:

I have a table named difficulties which have two fields: an id field (primary, auto-generated) and a type varchar field.

I'm running the following insert:

        return connection.manager.insert('difficulties', [
            { type: 'Easy' },
            { type: 'Medium' },
            { type: 'Hard' },
        ]);

To my surprise, this query was generated:

Screen Shot 2019-03-17 at 10 38 10 AM

The result is unexpected to me because I specified that I wanted to insert the type column, but Typeorm didn't specify that in the query.

Digging down through the code I found out this function, which appears to be the one where the issue is:

protected createColumnNamesExpression(): string {

// <some code>.

// in the case if there are no insert columns specified and table without metadata used
// we get columns from the inserted value map, in the case if only one inserted map is specified
if (!this.expressionMap.mainAlias!.hasMetadata && !this.expressionMap.insertColumns.length) {
  const valueSets = this.getValueSets();
  if (valueSets.length === 1)
    return Object.keys(valueSets[0]).map(columnName => this.escape(columnName)).join(", ");
 }

// <some code>.

if I remove the check if (valueSets.length === 1), Typeorm generates the correct query and the information is inserted successfully:

Screen Shot 2019-03-17 at 10 46 10 AM

My questions would be:

  1. Am I using the entity manager the wrong way? If so, how do I specify the columns that I want to insert into a table passing the table's name as a string?

  2. I don't have enough context about the code or understand the comment that was left to be able to make a PR removing it - perhaps it's fixing something that I'm not aware of. In that case, should we come up with a better solution, other than removing the check if (valueSets.length === 1)?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions