与sequelize和mysql重复字段相关的graphql

jucafojl  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(478)

https://www.graphqlbin.com/v2/glg9fp
我被这个问题困扰了一天,需要有人帮助。我有两个续集模型

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;
        }

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;
}

但是graphql总是返回一个错误:
“extensions”:{“code”:“internal \u server \u error”,“exception”:{“name”:“sequelizedatabaseerror”,“parent”:{“code”:“er \u bad \u field \u error”,“errno”:1054,“sqlstate”:“42s22”,“sqlmessage”:“field list”中的未知列“casacodcasa”,“sql”:“select” id , cod_casa , inicio , fim , preco_semana 作为 precoSemana , preco_dia 作为 precoDia , preco_fimsemana 作为 precoFimsemana , estadia_minima 作为 estadiaMinima , descricao , observacoes , createdAt , updatedAt ,来自 periodo 作为 periodo periodo在哪里 . casacodcasa=23;“},
问题似乎与定义有关。我还定义了解析器和graphql模式。有人干过这种事吗?提前谢谢。

p8h8hvxi

p8h8hvxi1#

在关系/关联的两个定义中,fix由o个自定义外键选项组成,例如:

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

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

正如@vivek doshi saind所说,但这两种型号都适用。

w3nuxt5m

w3nuxt5m2#

您需要定义外键自定义,因为您没有使用默认主键作为id

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

根据文件:

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

注:user.belongsto(公司);//<----将向用户添加公司的uuid,所以在您的情况下是casacodcasa

相关问题