2017-04-04 42 views
1
Error (E_UNKNOWN) :: Encountered an unexpected error 
: Trying to define a collection (external_resource) which already exists. 

我試圖創建現有數據庫的模型。我不使用帆。只需水線獨立。我有連接設置遷移安全。但是,waterline仍在嘗試創建已存在於我的預先存在的數據庫中的表。如果我讓tableName成爲一個新名字,它就可以正常工作。這個問題似乎是我嘗試使用現有表中的現有數據,然後爲其創建水線模型。水線現有的數據庫遷移到水線車型無帆

我相信這是一個錯誤,因爲遷移安全不應該試圖創建一個表的所有權利?

我正在使用沒有風帆的水線。

水線被配置是這樣的:

``` 
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 
    } 
``` 

使用帆-的PostgreSQL的方式。 「sails-postgresql」:「^ 0.11.4」,

我的一個假設是我的模型定義完全不符合我的postgresql模型。我試圖做到這一點,但也許我錯過了一些小事。在水線代碼中它檢查模型==模式定義?也許如果我知道這一點,我可以找到爲什麼這是試圖做一個創建

3年的用戶航行。在數十億的公司:(

在此先感謝主要生產水平的bug

更多信息:在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 
    } 
    } 
}); 

``` 

回答