在保存实体 PrimaryGeneratedColumn 值时未生成,给出 NOT NULL 约束错误
On saving entity PrimaryGeneratedColumn value not generated, gives NOT NULL constraint error
Typeorm 0.2.8
我正在构建一个同时用于移动和浏览器 (PWA) 的 Ionic 应用程序。下面是我项目中的一些简短代码。我创建了一个带有 PrimaryGeneratedColumn
的简单实体并尝试插入一个实例。这会生成有关主列为 NULL
的错误。 'generated' 一词不是表示生成了列值吗?
这是一个错误吗? sqljs
驱动程序特定的东西?或者我错过了一些明显而简单的事情?
实体
@Entity()
export class MyEntity {
@PrimaryGeneratedColumn()
id:number;
@Column()
name:string;
@CreateDateColumn()
createdAt:string;
}
迁移
export class Migration20181022133900 implements MigrationInterface {
async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.createTable(new Table({
name: 'my_entity',
columns: [
{
name: 'id',
type: 'int',
isPrimary: true,
isGenerated: true
},
{
name: 'name',
type: 'varchar'
},
{
name: 'createdAt',
type: 'timestamp',
'default': 'now()'
}
]
}), true);
}
async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.dropTable('my_entity');
}
}
数据库提供者
const DATABASE_SHARED_OPTIONS:Partial<ConnectionOptions> = {
entities: [
MyEntity
],
logging: 'all',
logger: new DatabaseLogger(),
migrationsRun: true,
migrations: [
Migration20181022133900
]
};
@Injectable()
export class Database {
constructor(public platform:Platform) {}
setup(): Promise<Connection> {
let options: CordovaConnectionOptions | SqljsConnectionOptions;
// Mobile app
if (this.platform.is('cordova')) {
options = {
type: 'cordova',
database: 'my_project.db',
location: 'default',
};
options = Object.assign(options, DATABASE_SHARED_OPTIONS);
}
// Browser PWA app
else {
options = {
type: 'sqljs',
autoSave: true,
location: 'my_project',
};
options = Object.assign(options, DATABASE_SHARED_OPTIONS);
}
return createConnection(options);
}
}
应用组件
export class MyApp {
constructor(
platform: Platform,
database: Database
) {
platform.ready().then(() => {
database.setup()
.then((connection) => {
this.insertTest();
});
});
}
insertTest() {
const myEntity= new MyEntity();
myEntity.name = 'foo';
getRepository(MyEntity).save(myEntity)
.then((data) => {
console.log(data); // never reached due to error
});
}
}
数据库日志显示以下查询(带参数["foo"]
):
INSERT INTO "my_entity"("name", "createdAt") VALUES (?, datetime('now'))
我的控制台中出现以下错误:
ERROR Error: Uncaught (in promise): QueryFailedError: NOT NULL constraint failed: my_entity.id
更新 1
似乎只有在使用迁移时才会出错。删除迁移并在数据库设置上使用 synchronize: true
有效并为实体生成 id
。那么我在迁移代码中的列定义有问题吗?
{
name: 'id',
type: 'int',
isPrimary: true,
isGenerated: true
}
更新 2
好的,我修好了。 @PrimaryGeneratedColumn
的迁移配置似乎非常具体。对于遇到此问题的任何其他人,这已为我修复:
{
name: 'id',
type: 'integer', // instead of 'int', required for the increment strategy
isPrimary: true,
isGenerated: true,
generationStrategy: 'increment' // thought this was the default
}
好的,我修好了。 @PrimaryGeneratedColumn
的迁移配置似乎非常具体。对于遇到此问题的任何其他人,这已为我修复:
{
name: 'id',
type: 'integer', // instead of 'int', required for the increment strategy
isPrimary: true,
isGenerated: true,
generationStrategy: 'increment' // thought this was the default
}
Typeorm 0.2.8
我正在构建一个同时用于移动和浏览器 (PWA) 的 Ionic 应用程序。下面是我项目中的一些简短代码。我创建了一个带有 PrimaryGeneratedColumn
的简单实体并尝试插入一个实例。这会生成有关主列为 NULL
的错误。 'generated' 一词不是表示生成了列值吗?
这是一个错误吗? sqljs
驱动程序特定的东西?或者我错过了一些明显而简单的事情?
实体
@Entity()
export class MyEntity {
@PrimaryGeneratedColumn()
id:number;
@Column()
name:string;
@CreateDateColumn()
createdAt:string;
}
迁移
export class Migration20181022133900 implements MigrationInterface {
async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.createTable(new Table({
name: 'my_entity',
columns: [
{
name: 'id',
type: 'int',
isPrimary: true,
isGenerated: true
},
{
name: 'name',
type: 'varchar'
},
{
name: 'createdAt',
type: 'timestamp',
'default': 'now()'
}
]
}), true);
}
async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.dropTable('my_entity');
}
}
数据库提供者
const DATABASE_SHARED_OPTIONS:Partial<ConnectionOptions> = {
entities: [
MyEntity
],
logging: 'all',
logger: new DatabaseLogger(),
migrationsRun: true,
migrations: [
Migration20181022133900
]
};
@Injectable()
export class Database {
constructor(public platform:Platform) {}
setup(): Promise<Connection> {
let options: CordovaConnectionOptions | SqljsConnectionOptions;
// Mobile app
if (this.platform.is('cordova')) {
options = {
type: 'cordova',
database: 'my_project.db',
location: 'default',
};
options = Object.assign(options, DATABASE_SHARED_OPTIONS);
}
// Browser PWA app
else {
options = {
type: 'sqljs',
autoSave: true,
location: 'my_project',
};
options = Object.assign(options, DATABASE_SHARED_OPTIONS);
}
return createConnection(options);
}
}
应用组件
export class MyApp {
constructor(
platform: Platform,
database: Database
) {
platform.ready().then(() => {
database.setup()
.then((connection) => {
this.insertTest();
});
});
}
insertTest() {
const myEntity= new MyEntity();
myEntity.name = 'foo';
getRepository(MyEntity).save(myEntity)
.then((data) => {
console.log(data); // never reached due to error
});
}
}
数据库日志显示以下查询(带参数["foo"]
):
INSERT INTO "my_entity"("name", "createdAt") VALUES (?, datetime('now'))
我的控制台中出现以下错误:
ERROR Error: Uncaught (in promise): QueryFailedError: NOT NULL constraint failed: my_entity.id
更新 1
似乎只有在使用迁移时才会出错。删除迁移并在数据库设置上使用 synchronize: true
有效并为实体生成 id
。那么我在迁移代码中的列定义有问题吗?
{
name: 'id',
type: 'int',
isPrimary: true,
isGenerated: true
}
更新 2
好的,我修好了。 @PrimaryGeneratedColumn
的迁移配置似乎非常具体。对于遇到此问题的任何其他人,这已为我修复:
{
name: 'id',
type: 'integer', // instead of 'int', required for the increment strategy
isPrimary: true,
isGenerated: true,
generationStrategy: 'increment' // thought this was the default
}
好的,我修好了。 @PrimaryGeneratedColumn
的迁移配置似乎非常具体。对于遇到此问题的任何其他人,这已为我修复:
{
name: 'id',
type: 'integer', // instead of 'int', required for the increment strategy
isPrimary: true,
isGenerated: true,
generationStrategy: 'increment' // thought this was the default
}