Creating hasMany association elements

For the first time, asking a question about stack overflows, I hope I formulate it correctly: I am trying to create a simple blog application as a homework when using Sequelize to manage a database. However, I had a problem trying to create some test data when I try to create elements related to my model. Here is my code, it should be the same syntax as in the documentation ( http://docs.sequelizejs.com/en/v3/docs/associations/#creating-elements-of-a-hasmany-or-belongstomany-association ) .

Current behavior: both users are created with all attributes, messages are not created at all, and no errors are reported.

masterdb.sync({force:true}).then( x => {
    return Promise.all([
        User.create({
            username:'Timothy',
            password:'plain-text',
            email:'x@msn.nl',
            Posts: [
                { body: 'Hello everyone, my name is Timothy.' },
                { body: 'Today was a good day.'}
            ]
        }, {
            include: [ Post ]
        }),
        User.create({
            username:'Jack Sparrow', 
            password:'plain-text', 
            email:'jacksparrow@msn.nl'
        }),
    ])
}).catch(x => console.log(x))

And here are my model declarations and relationships:

const Sequelize = require('sequelize')
const masterdb = new Sequelize('blogapplication', process.env.POSTGRES_USER, process.env.POSTGRES_PASSWORD, {
    dialect: 'postgres',
})
const User = masterdb.define('user', {
    username: {
        type: Sequelize.STRING,
        unique: true,
        allowNull: false
    },
    password: {
        type: Sequelize.STRING,
        allowNull: false
    },
    email: {
        type: Sequelize.STRING,
        unique: true,
        allowNull: false
    }
}, {
    paranoid: true
})

const Post = masterdb.define('post', {
    body: {
        type: Sequelize.STRING(9001),
        allowNull: false,
        unique:true,
    }
}, {
    paranoid: true
})

User.hasMany(Post)
+4
1

. Sequalize, , . :)

            posts: [
                { body: 'Hello everyone, my name is Timothy.' },
                { body: 'Today was a good day.'}
            ]

:

        Posts: [
            { body: 'Hello everyone, my name is Timothy.' },
            { body: 'Today was a good day.'}
        ]

?

- , P, p.

:

testdb=# select * from users; 
 id |   username   |  password  |       email        |         createdAt          |         updatedAt          | deletedAt 
----+--------------+------------+--------------------+----------------------------+----------------------------+-----------
  1 | Jack Sparrow | plain-text | jacksparrow@msn.nl | 2016-11-17 22:38:06.493+01 | 2016-11-17 22:38:06.493+01 | 
  2 | Timothy      | plain-text | x@msn.nl           | 2016-11-17 22:38:06.492+01 | 2016-11-17 22:38:06.492+01 | 
(2 rows)

testdb=# select * from posts; 
 id |                body                 |         createdAt          |         updatedAt          | deletedAt | userId 
----+-------------------------------------+----------------------------+----------------------------+-----------+--------
  1 | Hello everyone, my name is Timothy. | 2016-11-17 22:38:06.515+01 | 2016-11-17 22:38:06.515+01 |           |      2
  2 | Today was a good day.               | 2016-11-17 22:38:06.515+01 | 2016-11-17 22:38:06.515+01 |           |      2
(2 rows)

: , , .

:

const Post = masterdb.define('post', {

-, P.

, , Post Post, . , .

, docs of sequelize , . , ? "" t T.

, "" , . , - . , , . var Tag = this.sequelize.definte('tag', {.

var Tag = masterdb.define('tag', {.

, - this.sequelize?

. testdb , :

const Sequelize = require('sequelize')
const masterdb = new Sequelize('testdb', process.env.POSTGRES_USER, process.env.POSTGRES_PASSWORD, {
    dialect: 'postgres',
})
const User = masterdb.define('user', {
    username: {
        type: Sequelize.STRING,
        unique: true,
        allowNull: false
    },
    password: {
        type: Sequelize.STRING,
        allowNull: false
    },
    email: {
        type: Sequelize.STRING,
        unique: true,
        allowNull: false
    }
}, {
    paranoid: true
})

var Tag = masterdb.define('Tag', { //this is 'tag' in their tutorial
  name: Sequelize.STRING
}, {
    paranoid: true
})

User.hasMany(Tag)

masterdb.sync({force:true}).then( x => {
    return Promise.all([
        User.create({
            username:'Timothy',
            password:'plain-text',
            email:'x@msn.nl',
            Tags: [
            { name: 'Alpha'},
            { name: 'Beta'}
          ]
        }, {
          include: [ Tag ]
        }),
        User.create({
            username:'Jack Sparrow', 
            password:'plain-text', 
            email:'jacksparrow@msn.nl'
        }),
    ])
}).catch(x => console.log(x))
+2

Source: https://habr.com/ru/post/1661058/


All Articles