Vincit / objection-graphql

GraphQL schema generator for objection.js
MIT License
307 stars 34 forks source link

Tests don't pass for MySQL? #20

Closed Trellian closed 6 years ago

Trellian commented 6 years ago

On MySQL, the table creation does not happen successfully (because of the impossibly stupid handling of foreign keys in MySQL).

I had to modify the 'createTables' class as follows

`


  createTables() {
    const knex = this.knex;

    return knex.schema.dropTableIfExists('Person_Movie')    // changed order of dropping for MySQL
    .then(() => {
      return knex.schema.dropTableIfExists('Review');
    }).then(() => {
      return knex.schema.dropTableIfExists('Person');
    }).then(() => {
      return knex.schema.dropTableIfExists('Movie');
    }).then(() => {
      return knex.schema.createTable('Movie', (table) => {
        table.increments('id').primary().unsigned();
        table.string('name');
        table.date('release_date');
      });
    }).then(() => {
      return knex.schema.createTable('Person', (table) => {
        table.increments('id').primary().unsigned();
        table.string('firstName');
        table.string('lastName');
        table.enum('gender', _.values(models.Person.Gender));
        table.integer('age');
        table.json('addresses', true);
        table.integer('parentId')
          .unsigned()
          .nullable()
          .defaultTo(0)
//          .references('id')       /* delay creation of foreign key for mysql  */
//          .inTable('Person')
          .index();
      });
    }).then(() => {
        return knex.schema
           .raw('alter table Person add CONSTRAINT FOREIGN KEY (`parentId`) REFERENCES `Person` (`id`)');
    }).then(() => {
      return knex.schema.createTable('Review', (table) => {
        table.increments('id').primary().unsigned();
        table.string('title');
        table.integer('stars');
        table.string('text');
        table.integer('movieId')
          .unsigned()
          .nullable()
          .references('id')
          .inTable('Movie')
          .index();
        table.integer('reviewerId')
          .unsigned()
          .nullable()
          .references('id')
          .inTable('Person')
          .index();
      });
    }).then(() => {
      return knex.schema.createTable('Person_Movie', (table) => {
        table.increments('id').primary().unsigned().index();
        table.integer('movieId')
          .unsigned()
          .nullable()
          .references('id')
          .inTable('Movie')
          .index();
        table.integer('personId')
          .unsigned()
          .nullable()
          .references('id')
          .inTable('Person')
          .index();
      });
    });
  }
`
Even after this, though, there were many failures, I have attached a zip file with a log of the errors, please see attached
[test_errors.zip](https://github.com/Vincit/objection-graphql/files/1417732/test_errors.zip)
koskimas commented 6 years ago

Thanks! I'll fix this for the next release.

Trellian commented 6 years ago

@koskimas Thanks! MySQL is notorious for it's poor foreign key handling, for many years now. And I don't understand why knex has a 'dropForeign' method, but no 'addForeign' method. Weird.