【问题标题】:Loopback 4, auto create table from models?Loopback 4,从模型自动创建表?
【发布时间】:2019-09-18 11:44:41
【问题描述】:

我正在尝试使用 Strongloop Loopback 4 框架从模型中自动创建表。

我遇到的问题是模型属性都是小写的,即使它们是用 camelCase 定义的。

用户模型示例

@model({ name: 'users', settings: { strict: false }, excludeBaseProperties: ['password'] })
export class User extends Entity {
@property({
    type: 'number',
    id: true,
    required: true,
    generated: true,
})
id: number;

@property({
    type: 'string',
    required: true,
})
firstName: string;

@property({
    type: 'string',
    required: true,
})
lastName: string;

@property({
    type: 'string',
    required: true,
    index: {
        unique: true,
    },
})
email: string;

@property({
    type: 'string',
    required: true,
    index: {
        unique: true,
    },
})
username: string;

@property({
    type: 'string',
    required: true,
})
password: string;

[prop: string]: any;

constructor(data?: Partial<User>) {
    super(data);
 }
}

db.datasource.json

{
 "name": "db",
 "connector": "postgresql",
 "url": "postgres://postgres:postgres@localhost:5432/test_db",
 "host": "localhost",
 "port": 5432,
 "user": "postgres",
 "password": "postgres",
 "database": "test_db"
}

现在我在 package.json 中使用名为 migrate 的脚本或使用 automigrate() 得到了相同的结果。如您所见,名字和姓氏被定义为名字和姓氏,但是迁移完成后,它们被创建为名字和姓氏。有谁知道可能是什么问题?

【问题讨论】:

    标签: node.js loopback


    【解决方案1】:

    找到它,如果将来有人需要它。在@property 对象中添加这样的属性。

    @property({
        postgresql: {
            columnName: 'cammelCase', // it will be uppercased
        },
    })
    

    如果有人知道更好的方法,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2019-08-13
      • 2016-12-30
      • 2017-05-11
      • 1970-01-01
      相关资源
      最近更新 更多