Skip to content

foreign key cannot be primary on SQLite #9461

Closed

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

Activity

  1. Ettapp commented on May 21, 2018

    @Ettapp
    Author

    Just tested under MySQL and it work fine

  2. added
    type: bug
    on May 22, 2018
  3. sushantdhiman commented on May 22, 2018

    @sushantdhiman
    Contributor

    This should be fixed here

    dataTypeString = containsAutoIncrement ? 'INTEGER PRIMARY KEY AUTOINCREMENT' : 'INTEGER PRIMARY KEY';

    Entire attribute string is being replaced which contains `REFERENCES `files` (`id`)` at end. We need to keep REFERENCE part when doing this replacement

  4. added
    dialect: sqlite
    on May 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dialect: sqlitetype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions