1602 / jugglingdb

Multi-database ORM for nodejs: redis, mongodb, mysql, sqlite3, postgresql, arango, in-memory...
http://1602.github.io/jugglingdb/
2.04k stars 241 forks source link

Cannot override `foreignKey` for hasMany using `through` setting #346

Open sourcec0de opened 10 years ago

sourcec0de commented 10 years ago

As the title suggests

var Tag = schema.define('Tag', {
    name: String,
    parent_id: Number
}, {
    table: 'tags'
});

var Category = schema.define('Category', {
    name: String
}, {
    table: 'categories'
});

// Intersection Tables
var TagCategory = schema.define('TagCategory', {
    tag_id: Number,
    category_id: Number
}, {
    table: 'tag_categories'
});

// Relations
TagCategory.belongsTo(Tag);
TagCategory.belongsTo(Category);

Tag.belongsTo(Tag, {
    as: 'parent',
    foreignKey: 'parent_id'
});

Tag.hasMany(Category, {
    as: 'categories',
    through: TagCategory,
    foreignKey: 'tag_id'
})

Category.hasMany(Tag, {
    as: 'tags',
    through: TagCategory,
    foreignKey: 'category_id'
})

This results in table schema like this

sourcec0de commented 10 years ago

It automatically creates tagId and categoryId by default

shaunc commented 10 years ago

This is even worse if you want to have a many-to-many relationship between a table and itself: not only can't you control the names, but they are the same. :(

taoyuan commented 10 years ago

Any progress on this?