nocobase/nocobase · error · Error

Collection model ${this.collection.model.name} has no primar

Error message

Collection model ${this.collection.model.name} has no primary key attribute

What it means

BelongsToManyField.bind() refuses to create the association when the source collection's Sequelize model has no primary key attribute, because a many-to-many JOIN requires a source key to link through the pivot table. NocoBase throws this early at bind time instead of letting Sequelize produce confusing SQL errors later.

Source

Thrown at packages/core/database/src/fields/belongs-to-many-field.ts:135

    if (!this.keyPairsTypeMatched(otherKeyType, targetKeyType)) {
      throw new Error(
        `Other key "${otherKey}" type "${otherKeyType}" does not match target key "${targetKey}" type "${targetKeyType}" in belongs to many relation "${this.name}" of collection "${this.collection.name}"`,
      );
    }
  }

  bind() {
    const { database, collection } = this.context;

    const Target = this.TargetModel;

    if (!Target) {
      database.addPendingField(this);
      return false;
    }

    if (!this.collection.model.primaryKeyAttribute) {
      throw new Error(`Collection model ${this.collection.model.name} has no primary key attribute`);
    }

    if (!Target.primaryKeyAttribute) {
      throw new Error(`Target model ${Target.name} has no primary key attribute`);
    }

    this.checkAssociationKeys(database);

    const through = this.through;

    let Through: Collection;

    if (database.hasCollection(through)) {
      Through = database.getCollection(through);
      Through.options.sourceCollectionName ??= this.collection.name;
      Through.options.targetCollectionName ??= this.target;
    } else {
      const throughCollectionOptions = {

View on GitHub (pinned to fa42722fef)

Solutions

  1. Add a primary key field to the source collection (e.g. { name: 'id', type: 'bigInt', primaryKey: true, autoIncrement: true }).
  2. If the PK exists in the definition but not in the DB, run sync/`yarn nocobase upgrade` so the column is created.
  3. Check for a typo in the PK field name/options and confirm model.primaryKeyAttribute resolves.
  4. Restore the dropped PK column via migration if a previous change removed it.
  5. Rebind the association after the PK exists.

Example fix

// before
database.collection({ name: 'posts', fields: [
  { name: 'title', type: 'string' },
]});
// after
database.collection({ name: 'posts', fields: [
  { name: 'id', type: 'bigInt', primaryKey: true, autoIncrement: true },
  { name: 'title', type: 'string' },
]});
Defensive patterns

Strategy: validation

Validate before calling

function assertHasPrimaryKey(db, collectionName) {
  const c = db.getCollection(collectionName);
  if (!c || !c.model.primaryKeyAttribute) {
    throw new Error(`Collection ${collectionName} must define a primary key field before relations bind`);
  }
}
// call before adding belongsToMany fields

Type guard

const hasPrimaryKey = (collection) =>
  Boolean(collection && collection.model && collection.model.primaryKeyAttribute);

Try / catch

try {
  db.bindField('posts', 'tags');
} catch (e) {
  if (e.message.includes('has no primary key attribute')) {
    db.collection({ name: 'posts', fields: [{ name: 'id', type: 'bigInt', primaryKey: true, autoIncrement: true }] });
  } else throw e;
}

Prevention

When it happens

Trigger: Calling database.collection() / addField with a belongsToMany field on a collection whose model has no primaryKeyAttribute — typically a collection defined without any primary key field, or whose PK was removed/renamed — when the field is bound (bind() after the target exists).

Common situations: Defining a collection where every field is non-primary (all type options missing primaryKey: true); a migration or dump/restore that dropped the PK column; a custom collection whose fields failed to sync so the PK column doesn't exist; typo in the pk field name so it isn't recognized.

Related errors


AI-assisted analysis of nocobase/nocobase@fa42722fef (2026-09-01). Data as JSON: /api/errors/075682ca76d1e1aa. Report an issue: GitHub.