Skip to content

Manager insert query with auto-generated columns #3837

Open

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)?

Activity

  1. Kononnable commented on May 1, 2019

    @Kononnable
    Contributor

    I cannot reproduce your issue, even on specified version. Can you provide reproduction repo/entity definition you're reproducing the issue?

  2. clovis1122 commented on May 2, 2019

    @clovis1122
    ContributorAuthor

    @Kononnable, thanks for showing interest in this issue. Added failing tests on #4070. I first tried to reproduce it using entities and TypeORM generated the correct query. Seems like the issue happens when there's not enough information related to the table?

    In my particular case, we're modeling the "difficulties" as enums so there's no Entity object.

  3. diegosalasmartinez commented on Mar 25, 2023

    @diegosalasmartinez

    I don't know if this can help somebody but the same happened to me, the query wasn't generating properly. So, I created a query builder and added the columns (as an array string) to the table name.

    Like this

    await queryRunner.manager
        .createQueryBuilder()
        .insert()
        .into('tableName', ["type"])
        .values([...data])
        .execute()
    
  4. github-actions commented on Sep 18, 2025

    @github-actions

    TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).

    As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.

    🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
    If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.

  5. alumni commented on Sep 19, 2025

    @alumni
    Collaborator

    Reopening because we have a test that references this issue

  6. reopened this on Sep 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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