0

https://www.graphqlbin.com/v2/gLg9FP

I am stuck with this problem for a day now and need somebody help. I have 2 sequelize models

      module.exports = (sequelize, DataTypes) => {
            const Periodo = sequelize.define('periodo', {
...
                    cod_casa: {
                        type: DataTypes.INTEGER(11),
                        allowNull: false,
                        field: 'cod_casa'
                    },
                   ....
                },
     ...
            );

            Periodo.associate = (models) => {
                Periodo.belongsTo(models.casa);
            };

            return Periodo;
        }

and

  const Casa = sequelize.define('casa', {
                cod_casa: {
                    type: DataTypes.INTEGER(11),
                    allowNull: false,
                    primaryKey: true,
                    autoIncrement: true,
                    field: 'cod_casa'
                },

.....
    Casa.associate = (models) => {
        Casa.hasMany(models.periodo);
    };

    return Casa;
}

But graphQl always return an error :

"extensions": { "code": "INTERNAL_SERVER_ERROR", "exception": { "name": "SequelizeDatabaseError", "parent": { "code": "ER_BAD_FIELD_ERROR", "errno": 1054, "sqlState": "42S22", "sqlMessage": "Unknown column 'casaCodCasa' in 'field list'", "sql": "SELECT id, cod_casa, inicio, fim, preco_semana AS precoSemana, preco_dia AS precoDia, preco_fimsemana AS precoFimsemana, estadia_minima AS estadiaMinima, descricao, observacoes, createdAt, updatedAt, casaCodCasa FROM periodo AS periodo WHERE periodo.casaCodCasa = 23;" },

The problem seems to be in relation definition. I also have the resolvers and graphQL schema well defined. Anybody been trougth someting like this? Thanks in advance.

2 Answers 2

1

You need to define foreign key custom as you are not using default primary key as id

Periodo.belongsTo(models.casa,{foreignKey: 'cod_casa'}); // <--- HERE

As per the DOC :

const User = this.sequelize.define('user', {/* attributes */})
const Company  = this.sequelize.define('company', {/* attributes */});

User.belongsTo(Company); // Will add companyId to user

const User = this.sequelize.define('user', {/* attributes */}, {underscored: true})
const Company  = this.sequelize.define('company', {
  uuid: {
    type: Sequelize.UUID,
    primaryKey: true
  }
});

User.belongsTo(Company); // <----- Will add company_uuid to user

NOTE : User.belongsTo(Company); // <----- Will add company_uuid to user , so in your case its casaCodCasa

Sign up to request clarification or add additional context in comments.

6 Comments

I am using primary key const Periodo = sequelize.define('periodo', { id: { type: DataTypes.INTEGER(11), allowNull: false, primaryKey: true, autoIncrement: true, field: 'id' }, cod_casa: { type: DataTypes.INTEGER(11), allowNull: false, field: 'cod_casa' },
Yes but not in casa table, that's why just change the line as I have suggested and run your code.
i ve changed the line ... no effect at all. Maybe problem is because primary key of Casa have same name foreign key of Periodo?
cannot share localhost
if i use query { periodos { casaid } }
|
0

Fix consists of o custom foreign key option in both definition of the relation /association ex:

Periodo.associate = (models) => {
        Periodo.belongsTo(models.casa,**{foreignKey: 'cod_casa'}**);
    };

and

Casa.associate = (models) => {
        Casa.hasMany(models.periodo,**{ foreignKey: 'cod_casa' }**);
        Casa.hasMany(models.feedback,{ foreignKey: 'cod_casa' });
    };

as @Vivek Doshi saind but for both models.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.