Closed reversefuture closed 4 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.
I am facing the same issue. Any chance, this is going to get fixed soon.
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
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
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.
I think this is fixed in sequelize-auto@0.5.0
Please try it and close this issue if so. Thanks!
Fixed in sequelize-auto@0.5.1
.
Mh weird. It seems it's not fixed. I'm using postgress and sequelize-auto@0.7.2 and the id column is always
But i'm sure I declared it as autoIncrement in Database
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
), KEYposition_id
(ad_position_id
), KEYenabled
(enabled
) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;