Merge pull request #303 from strongloop/fix/fk-docs
Fix foreign key example to avoid constraint errors
This commit is contained in:
commit
118bf9aee6
57
README.md
57
README.md
|
@ -363,21 +363,26 @@ Example:
|
||||||
{
|
{
|
||||||
"name": "Book",
|
"name": "Book",
|
||||||
"base": "PersistedModel",
|
"base": "PersistedModel",
|
||||||
"idInjection": true,
|
"idInjection": false,
|
||||||
"properties": {
|
"properties": {
|
||||||
|
"bId": {
|
||||||
|
"type": "number",
|
||||||
|
"id": true,
|
||||||
|
"required": true
|
||||||
|
},
|
||||||
"name": {
|
"name": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}, "isbn": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
},
|
||||||
|
"isbn": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"validations": [],
|
"validations": [],
|
||||||
"relations": {
|
"relations": {
|
||||||
"author": {
|
"author": {
|
||||||
"type": "belongsTo",
|
"type": "belongsTo",
|
||||||
"model": "Author",
|
"model": "Author",
|
||||||
"foreignKey": "authorId",
|
"foreignKey": "authorId"
|
||||||
"primaryKey": "id"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"acls": [],
|
"acls": [],
|
||||||
|
@ -386,7 +391,7 @@ Example:
|
||||||
"authorId": {
|
"authorId": {
|
||||||
"name": "authorId",
|
"name": "authorId",
|
||||||
"foreignKey": "authorId",
|
"foreignKey": "authorId",
|
||||||
"entityKey": "id",
|
"entityKey": "aId",
|
||||||
"entity": "Author"
|
"entity": "Author"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -397,33 +402,24 @@ Example:
|
||||||
{
|
{
|
||||||
"name": "Author",
|
"name": "Author",
|
||||||
"base": "PersistedModel",
|
"base": "PersistedModel",
|
||||||
"idInjection": true,
|
"idInjection": false,
|
||||||
"properties": {
|
"properties": {
|
||||||
|
"aId": {
|
||||||
|
"type": "number",
|
||||||
|
"id": true,
|
||||||
|
"required": true
|
||||||
|
},
|
||||||
"name": {
|
"name": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}, "dob": {
|
},
|
||||||
|
"dob": {
|
||||||
"type": "date"
|
"type": "date"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"validations": [],
|
"validations": [],
|
||||||
"relations": {
|
"relations": {},
|
||||||
"books": {
|
|
||||||
"type": "hasMany",
|
|
||||||
"model": "Book",
|
|
||||||
"foreignKey": "bookId",
|
|
||||||
"primaryKey": "id"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"acls": [],
|
"acls": [],
|
||||||
"methods": {},
|
"methods": {}
|
||||||
"foreignKeys": {
|
|
||||||
"bookId": {
|
|
||||||
"name": "bookId",
|
|
||||||
"foreignKey": "bookId",
|
|
||||||
"entityKey": "id",
|
|
||||||
"entity": "Book"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -434,11 +430,16 @@ module.exports = function(app) {
|
||||||
var Book = app.models.Book;
|
var Book = app.models.Book;
|
||||||
var Author = app.models.Author;
|
var Author = app.models.Author;
|
||||||
|
|
||||||
mysqlDs.automigrate(function(err) {
|
// first autoupdate the `Author` model to avoid foreign key constraint failure
|
||||||
|
mysqlDs.autoupdate('Author', function(err) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
console.log('\nAutoupdated table `Author`.');
|
||||||
|
|
||||||
// at this point the database tables `Book` and `Author`
|
mysqlDs.autoupdate('Book', function(err) {
|
||||||
// should have the foreign keys (`bookId` and `authorId`) integrated
|
if (err) throw err;
|
||||||
|
console.log('\nAutoupdated table `Book`.');
|
||||||
|
// at this point the database table `Book` should have one foreign key `authorId` integrated
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in New Issue