Skip to content

foreign key cannot be primary on SQLite #9461

@Ettapp

Description

@Ettapp

What are you doing?

Similarly to #5769 , I have two models with a one to one relation:

  • The File model, that hold the data of the file (weight, ...)
  • The Image model that hold image-specific data (height, width, ...)

Image.id should be the primary key of Image AND a foreign key to file.id, so I tried this:

const File = sequelize.define('file', {});
const Image = sequelize.define('image', {
  id: {
    primaryKey: true,
    type: Sequelize.INTEGER,
    references: {
      model: File,
      key: "id",
    }
  }
});

(the same things happen if I use belongsTo instead of referencing the File model in the define method)

What do you expect to happen?

I want the Image.id field to reference the File.id field.

It work if I do it on an other field (not the primary key), and that produce SQL like:

`file_id` INTEGER REFERENCES `files` (`id`) ON DELETE SET NULL ON UPDATE CASCADE

What is actually happening?

The file.id field is not referenced, and the produced SQL is like:

`id` INTEGER PRIMARY KEY

Dialect: sqlite3
Dialect version: 4.0.0
Sequelize version: 4.37.8
Tested with latest release: No

Metadata

Metadata

Assignees

No one assigned

    Labels

    dialect: sqliteFor issues and PRs. Things that involve SQLite (and do not involve all dialects).type: bugDEPRECATED: replace with the "bug" issue type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions