Search code examples
mysqlnestjstypeorm

Typeorm Entity Insert Without Synchronization


I'm using TypeORM and NestJs to work with an existing MySql database. There is a shared table that I must insert records into, which has an auto-incrementing primary key. Since the table is shared, I have synchronize turned off.

When I setup my TypeORM entity with the @PrimaryGeneratedColumn decorator, I get an error stating that the column "doesn't have a default value". If I change the decorator to @Column, then I receive an error regarding no primary key.

Can anyone tell me how I should setup my TypeORM entity so that I may insert a record into a table with a auto-incrementing column, where synchronize is turned off?

UPDATE

Here's a excerpt of the MySql table DDL:

CREATE TABLE IF NOT EXISTS tIntegrationItem (
    IntegrationItemID       mediumint AUTO_INCREMENT    NOT NULL,
    Description             varchar(200)                NOT NULL,
    CreatedDateTime         dateTime                    NOT NULL,
    PRIMARY KEY (IntegrationItemID)
) ENGINE=InnoDB;

Here's an excerpt of the entity:

@Entity({ name: "tIntegrationItem" })
export class IntegrationItem {
    @PrimaryGeneratedColumn()
    integrationItemId : number;

    @Column()
    description : string;

    @Column()
    createdDateTime  : Date = new Date();
}

Here's the error that I receive:

[Nest] 26248 - 01/22/2020, 2:18:43 PM [ExceptionsHandler] ER_NO_DEFAULT_FOR_FIELD: Field 'IntegrationItemID' doesn't have a default value +18075ms QueryFailedError: ER_NO_DEFAULT_FOR_FIELD: Field 'IntegrationItemID' doesn't have a default value


Solution

  • I switched the database configuration to a different database, and the application worked fine. I had the application configured to work with a local instance of MySql, which was not setup properly. I don't know the exact reason, though.

    UPDATE

    I've since learned that my local database did not match the server instance. When it was setup, the primary key column was not setup to auto-increment. *smh*