Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

afterCreate() in Sub-Object Creates Weird Error #135

Open
travistrue2008 opened this issue Aug 8, 2018 · 1 comment
Open

afterCreate() in Sub-Object Creates Weird Error #135

travistrue2008 opened this issue Aug 8, 2018 · 1 comment

Comments

@travistrue2008
Copy link

We have a pretty denormalized schema, so we need to make a LOT of association calls to create a single object... When I get to the 3rd association, I get this error by simply defining afterCreate(): TypeError: Cannot read property 'get' of undefined in my 3rd-level factory.

Here's my example:

// STEP 1: create a customer record
factory.define('customer', models.Customer, {
    company_id: factory.assoc('company', 'id'), // NOTE: this code is skipped because it works fine
  }, {
    async afterCreate (model, attrs, buildOptions) {
      // attempting to create a user record for the customer
      const user = await factory.create('user', { customer_id: model.id })
      console.info('user:', user)
    },
  },
)

// STEP 2: create a user record
factory.define('user', models.User, {
  contact_id: factory.assoc('contact', 'id'), // create a contact
  username: factory.sequence('User.username', (n) => `Username${n}`),
})

// STEP 3: create a contact record
factory.define('contact', models.Contact, {
    title: 'Sir',
    first_name: factory.sequence('Contact.first_name', (n) => `FirstName{n}`),
    last_name: factory.sequence('Contact.last_name', (n) => `LastName{n}`),
    notes: 'No thanks',
    avatar: 'www.avatar.com/something.jpeg',
  }, {
    // defining this function causes the error
    async afterCreate (model, attrs, buildOptions) {
      // TODO: define even more sub-models...
      return Promise.resolve()
    },
  },
)

We're using Sequelize as our ORM as well. I tried Googling for answers, but couldn't find any solutions.

@ws-perelyaynen
Copy link

for some reasons in case when factory-girl build schema doesn't match DB-schema, FG doesn't throw error. It just returns undefined. So guys who still uses this project : make sure your build schema fits to DB schema.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants