Skip to content

Instantly share code, notes, and snippets.

@qfox
Last active August 29, 2015 14:03
Show Gist options
  • Save qfox/865182f3b6e0c554514b to your computer and use it in GitHub Desktop.
Save qfox/865182f3b6e0c554514b to your computer and use it in GitHub Desktop.
problems in fieldName redefine
Executing (default): SELECT "forum_topic".*, "Comments"."id" AS "Comments.id", "Comments"."status" AS "Comments.status", "Comments"."title" AS "Comments.title", "Comments"."topicId" AS "Comments.topicId", "Comments"."sys_created" AS "Comments.sys_created", "Comments"."sys_modified" AS "Comments.sys_modified", "Comments"."parent" AS "Comments.parent" FROM "forum_topic" LEFT OUTER JOIN "forum_comment" AS "Comments" ON "forum_topic"."id" = "Comments"."parent";
events.js:72
throw er; // Unhandled 'error' event
^
error: relation "forum_topic" does not exist
at Connection.parseE (/home/qfox/web/megafon/selfcare-buddy/node_modules/pg/lib/connection.js:558:11)
at Connection.parseMessage (/home/qfox/web/megafon/selfcare-buddy/node_modules/pg/lib/connection.js:387:17)
at null.<anonymous> (/home/qfox/web/megafon/selfcare-buddy/node_modules/pg/lib/connection.js:92:20)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
at emitReadable (_stream_readable.js:404:5)
at readableAddChunk (_stream_readable.js:165:9)
at Socket.Readable.push (_stream_readable.js:127:10)
'use strict';
// test
var SEQUELIZE = require('sequelize');
// db
var sequelize = new SEQUELIZE('postgres://test:@localhost/test', { logging : console.log });
/**
* models
*/
var Topic = sequelize.define('ForumTopic', {
id : { type: SEQUELIZE.INTEGER, autoIncrement: true, primaryKey: true },
status : SEQUELIZE.BOOLEAN,
title : SEQUELIZE.STRING
}, {
tableName : 'forum_topic',
createdAt : 'sys_created',
updatedAt : 'sys_modified',
deletedAt : false
});
var Comment = sequelize.define('ForumComment', {
id : { type: SEQUELIZE.INTEGER, autoIncrement: true, primaryKey: true },
status : SEQUELIZE.BOOLEAN,
title : SEQUELIZE.STRING,
topicId : { type: SEQUELIZE.INTEGER, references: Topic, referencesKey: "id", field: 'parent' }
}, {
tableName : 'forum_comment',
createdAt : 'sys_created',
updatedAt : 'sys_modified',
deletedAt : false
});
/**
* references
*/
Comment.belongsTo(Topic, { as : 'Topic', foreignKey : 'parent' });
Topic.hasMany(Comment, { as : 'Comments', foreignKey : 'parent' });
/**
* action
*/
sequelize.sync({force : true}).done(function () {
// this is where we continue ...
Topic.findAll({ include: [ { model: Comment, as : 'Comments' } ] })
.success(function (topicsWithComment) {
console.log(JSON.stringify(topicsWithComment));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment