Skip to content
This repository was archived by the owner on Sep 27, 2021. It is now read-only.
This repository was archived by the owner on Sep 27, 2021. It is now read-only.

More seeder examples needed #404

Description

@andrewwippler

I am new to adonisjs (while not necessarily new to MVCs) and I found it difficult to seed a many-to-many relationship. (i.e. it took me over 24 hours to get the answer).

The error I was getting was:

From previous event:
    at Runner.run (/usr/src/app/node_modules/knex/lib/runner.js:34:31)
    at Builder.Target.then (/usr/src/app/node_modules/knex/lib/interface.js:20:43) {
  code: 'ER_BAD_FIELD_ERROR',
  errno: 1054,
  sqlMessage: "Unknown column '__setters__' in 'field list'",
  sqlState: '42S22',
  index: 0,
  sql: "insert into `ill_tags` (`illustration_id`, `tag_id`) values (2, `__setters__` = '$attributes', '$persisted', 'primaryKeyValue', '$originalAttributes', '$relations', '$sideLoaded', '$parent', '$frozen', '$visible', '$hidden', `$attributes` = '[object Object]', `$persisted` = false, `$originalAttributes` = '[object Object]', `$relations` = '[object Object]', `$sideLoaded` = '[object Object]', `$parent` = NULL, `$frozen` = false, `$visible` = 'id', 'name', `$hidden` = NULL)"

I was defining my seeder as the following to get the issue:

const illustrations = await Factory.model('App/Models/Illustration').createMany(5)
const tags = await Factory.model('App/Models/Tag').createMany(10)

await illustrations[0].tags().attach(tags[0])

The solution (to specify id) was found on issue adonisjs/lucid#178 -

await illustrations[0].tags().attach(tags[0].id)

So I propose adding to the seeder documentation the following:

const users = await Factory.model('App/Models/User').createMany(5)
const cars = await Factory.model('App/Models/Car').createMany(10)

// single car
await users[0].cars().attach(cars[0].id)
    
// all cars
const allCars = cars.map(car => car.id);
await users[0].cars().attach(allCars);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions