sequelize / sequelize-auto

Automatically generate bare sequelize models from your database.
2.91k stars 529 forks source link

sequelize-auto can't map AUTO_INCREMENT constraint #248

Closed reversefuture closed 4 years ago

reversefuture commented 6 years ago

When I run sequelize-auto, the generated mysql table mapping models don't have "autoIncrement: true' constraint. I tried several times. All failed. Below is the mysql creat table code: CREATE TABLE nideshop_ad ( id smallint(5) unsigned NOT NULL AUTO_INCREMENT, ad_position_id smallint(5) unsigned NOT NULL DEFAULT '0', media_type tinyint(3) unsigned NOT NULL DEFAULT '0', name varchar(60) NOT NULL DEFAULT '', link varchar(255) NOT NULL DEFAULT '', image_url text NOT NULL, content varchar(255) NOT NULL DEFAULT '', end_time int(11) NOT NULL DEFAULT '0', enabled tinyint(3) unsigned NOT NULL DEFAULT '1', PRIMARY KEY (id), KEY position_id (ad_position_id), KEY enabled (enabled) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

NobleUplift commented 5 years ago

I believe this might be a limit of sequelize, not just sequelize-auto. I tried to fetch the autoincrement value on save and no matter what I did, I couldn't get it to work.

https://stackoverflow.com/questions/52374889/how-do-i-get-the-mysql-autoincrement-primary-key-from-sequelize-on-save

nramaiah commented 5 years ago

I am facing the same issue. Any chance, this is going to get fixed soon.

aruntk commented 5 years ago

until this is fixed you can use @tkay/sequelize-auto

npm install --save @tkay/sequelize-auto

if you are using yarn

yarn add @tkay/sequelize-auto

polarathene commented 5 years ago

When I run sequelize-auto, the generated mysql table mapping models don't have "autoIncrement: true' constraint. I tried several times

@reversefuture There doesn't appear to be any updates to the repo since your issue, but I seem to get autoIncrement: true in my generated models no problem. I'm using mariadb but afaik that's pretty much compatible with mysql, the database I'm using is via a docker container which ships a non-default setup(the containers readme explains what and why), and I have INNODB disabled(which may be real difference in behaviour?)

docker run --rm -p 3306:3306 -e "MYSQL_ROOT_PASSWORD=<password>" -e "SKIP_INNODB=yes" -v ${PWD}/mysql:/var/lib/mysql --name database-mariadb jbergstroem/mariadb-alpine

Here's a generated example:

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('mbb_users', {
    uid: {
      type: DataTypes.INTEGER(10).UNSIGNED,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },

...

Perhaps you could migrate your database? The original data for the one I'm working with used to be on mysql a couple years ago.


EDIT: Oh nvm, the fix is in this PR over here specific to the mysql dialect: https://github.com/sequelize/sequelize-auto/pull/359/files

FredLackeyOfficial commented 4 years ago

Take a look at file-line-replacer when you have a moment. I've had the same problem for years and cannot stand dealing with writing scripts to tweak the final files. I created file-line-replacer to make fixing these files easier. This allows sequelize-auto to be used, without issue, and then adjust the files when you're finished.

Here are the steps I use. First, install it (I prefer installing it globally)...

npm i -g file-line-replacer

... and then, from inside of the project using sequelize, I run ...

file-line-replacer \
  --search-dir "/Users/flackey/my-project/src/data/models" \
  --backup-dir "/Users/flackey/my-project/_backup" \
  --old-lines "allowNull: false,|primaryKey: true" \
  --new-lines "autoIncrement: true,|primaryKey: true" \
  --overwrite

The above command will tweak your models, adding the autoIncrement key, and create a backup for each file before it is modified.

steveschmitt commented 4 years ago

I think this is fixed in sequelize-auto@0.5.0
Please try it and close this issue if so. Thanks!

steveschmitt commented 4 years ago

Fixed in sequelize-auto@0.5.1.

devonik commented 3 years ago

Mh weird. It seems it's not fixed. I'm using postgress and sequelize-auto@0.7.2 and the id column is always

grafik

But i'm sure I declared it as autoIncrement in Database