gpt4 book ai didi

node.js - Waterline 将现有数据库迁移到无 sails 的水线模型

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:43 24 4
gpt4 key购买 nike

 Error (E_UNKNOWN) :: Encountered an unexpected error
: Trying to define a collection (external_resource) which already exists.

我正在尝试为现有数据库创建模型。我没有使用 sails 。只是水线独立。我已将连接设置为安全迁移。然而,waterline 仍在尝试创建一个已存在于我预先存在的数据库中的表。如果我为 tableName 指定一个新名称,它就可以正常工作。问题似乎是当我尝试使用现有表中的现有数据,然后为其创建水线模型时。

我相信这是一个错误,因为迁移安全根本不应该尝试创建表,对吗?

我使用的是没有 sails 的水线。

水线配置如下:

```
models = [];
fs.readdirSync(HOMEDIR + '/lib/models/waterline').forEach(function(file) {
models.push(require(HOMEDIR + '/lib/models/waterline/' + file));
});

module.exports = {
init: function(next) {
models.forEach(function(model) {
orm.loadCollection(model);
});

orm.initialize(config, function(err, models) {
if (err) throw err;
global.models = models.collections;
global.connections = models.connections;
next();
});
}
};


//And this in my config
localhost: {
migrate: 'safe',
adapter: 'postgres',
database: 'intellinote',
host: 'localhost',
user: 'postgres',
password: '',
port: 5432
}
```

顺便使用 sails-postgresql。 "sails-postgresql": "^0.11.4",

我的一个假设是我的模型定义与我的 postgresql 模型不完全匹配。我尝试过这样做,但也许我错过了一些小事情。它在水线代码中的哪个位置检查 model == schema 定义?也许如果我知道这一点,我就能找到为什么要尝试创建

Sails 用户 3 年。值(value)数十亿美元的公司的主要生产级别错误:(

提前致谢

更多信息:表的 SQL 如下所示

```
DROP TABLE IF EXISTS "intellinotedb"."external_resource";
CREATE TABLE "intellinotedb"."external_resource" (
"id" int8 NOT NULL DEFAULT nextval('external_resource_id_seq'::regclass),
"external_id" varchar(2000) NOT NULL COLLATE "default",
"version_id" varchar(2000) COLLATE "default",
"url" varchar(5000) COLLATE "default",
"name" varchar(4000) COLLATE "default",
"size" int8,
"creator" varchar(50) NOT NULL COLLATE "default",
"created_at" timestamp(6) NOT NULL DEFAULT now(),
"modified_at" timestamp(6) NULL,
"project_id" int8 NOT NULL,
"note_id" int8,
"type" varchar(50) NOT NULL COLLATE "default",
"is_public" bool NOT NULL DEFAULT false,
"state" varchar(100) NOT NULL DEFAULT 'ACTIVE'::character varying COLLATE "default",
"mime_type" text COLLATE "default",
"internal_type" text COLLATE "default",
"is_template" bool NOT NULL,
"has_filled_data" bool NOT NULL
)
WITH (OIDS=FALSE);
ALTER TABLE "intellinotedb"."external_resource" OWNER TO "intellinote";

-- ----------------------------
-- Primary key structure for table external_resource
-- ----------------------------
ALTER TABLE "intellinotedb"."external_resource" ADD PRIMARY KEY ("id") NOT DEFERRABLE INITIALLY IMMEDIATE;

-- ----------------------------
-- Uniques structure for table external_resource
-- ----------------------------
ALTER TABLE "intellinotedb"."external_resource" ADD CONSTRAINT "external_resource_note_id_key" UNIQUE ("note_id") NOT DEFERRABLE INITIALLY IMMEDIATE;

-- ----------------------------
-- Foreign keys structure for table external_resource
-- ----------------------------
ALTER TABLE "intellinotedb"."external_resource" ADD CONSTRAINT "external_resource_note_id_fkey" FOREIGN KEY ("note_id") REFERENCES "intellinotedb"."note" ("id") ON UPDATE NO ACTION ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE;
ALTER TABLE "intellinotedb"."external_resource" ADD CONSTRAINT "external_resource_project_id_fkey" FOREIGN KEY ("project_id") REFERENCES "intellinotedb"."project" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION NOT DEFERRABLE INITIALLY IMMEDIATE;
```

还有我的模型:

```
Waterline = require('waterline');

module.exports = Waterline.Collection.extend({
tableName: 'external_resource',
meta: {
schemaName: process.env.WATERLINE_SCHEMA || 'intellinotedb'
},
connection: process.env.WATERLINE_DB || 'localhost',
attributes: {
id: {
type: 'integer',
autoIncrement: true,
primaryKey: true,
unique: true,
size: 8
},
external_id: {
type: 'string',
required: true,
size: 2000
},
version_id: {
type: 'string',
size: 2000
},
url: {
type: 'string',
required: true,
size: 5000
},
name: {
type: 'string',
required: true,
size: 4000
},
size: {
type: 'integer',
required: true,
size: 8
},
creator: {
type: 'string',
required: true,
size: 50
},
createdAt: {
type: 'datetime',
columnName: 'created_at'
},
updatedAt: {
type: 'datetime',
columnName: 'modified_at'
},
project_id: {
type: 'integer',
required: true
},
note_id: {
type: 'integer',
required: true,
size: 8
},
type: {
type: 'string',
defaultsTo: 'FILE',
required: true,
size: 50
},
is_public: {
type: 'boolean',
defaultsTo: true,
required: true
},
state: {
type: 'string',
enum: ['ACTIVE', 'DELETED'],
defaultsTo: 'ACTIVE',
required: true,
size: 100
},
mime_type: {
type: 'string',
required: true
},
internal_type: {
type: 'string',
defaultsTo: 'REGULAR',
required: true
},
is_template: {
type: 'boolean',
defaultsTo: false,
required: false
},
has_filled_data: {
type: 'boolean',
defaultsTo: false,
required: false
}
}
});

```

最佳答案

迁移功能需要位于每个模型上,而不是连接上。根据此线程:https://github.com/balderdashy/waterline/issues/1472

关于node.js - Waterline 将现有数据库迁移到无 sails 的水线模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43214030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com