sequelize-auto icon indicating copy to clipboard operation
sequelize-auto copied to clipboard

DataTypes.UUID with mysql

Open schealex opened this issue 4 years ago • 5 comments

Hey guys,

i've got my tables all having a varchar(36) as primary key containing the UUID of the object. The defaultValue is "(UUID())" which commands mysql to generate the uuid on creation.

Now when i try to auto generate using this package i'll get:

    id: {
      type: DataTypes.STRING(36),
      allowNull: false,
      defaultValue: Sequelize.Sequelize.fn('uuid'),
      primaryKey: true
    },

what i would like would be that sequilize-auto used the DataTypes.UUID as type. I looked at the source and it seems like the only way to get the UUID type is if the field is of type uuid | uniqueidentifier, both of which do not exist in mysql.

Am i being dumb or blind?

schealex avatar Jul 20 '21 15:07 schealex

So the if the column's default value is "(UUID())" then make the type DataTypes.UUID. That makes sense.

What should the Sequelize model's defaultValue be?

steveschmitt avatar Jul 20 '21 16:07 steveschmitt

Well now it depends, you could see it as an indicator that sequelize should take care of it and generate the uuid otherwise like in my above example it should call the function. I don't actually now if that works though, I've not tested this! But it would be awesome if we could make this integration quickly as my whole application depends on this. If I can support in any way, let me know. Thanks

schealex avatar Jul 20 '21 17:07 schealex

Perhaps you could manually code a model class or two and test it different ways, and let me know what works. Then I can put those changes into sequelize-auto.

steveschmitt avatar Jul 20 '21 17:07 steveschmitt

Should we have something like this?

if (dialect == 'mysql' && fieldType == 'varchar(36)' && defaultValue.includes('uuid')) {
  dataType = DataTypes.UUID;
}

steveschmitt avatar Oct 18 '21 18:10 steveschmitt

Hey guys,

i've got my tables all having a varchar(36) as primary key containing the UUID of the object. The defaultValue is "(UUID())" which commands mysql to generate the uuid on creation.

Now when i try to auto generate using this package i'll get:

    id: {
      type: DataTypes.STRING(36),
      allowNull: false,
      defaultValue: Sequelize.Sequelize.fn('uuid'),
      primaryKey: true
    },

what i would like would be that sequilize-auto used the DataTypes.UUID as type. I looked at the source and it seems like the only way to get the UUID type is if the field is of type uuid | uniqueidentifier, both of which do not exist in mysql.

Am i being dumb or blind?

I just want to konw : How to set mysql table default value, then I can run generate script to get " defaultValue:DataTypes.UUIDV4" like left code

I have try set "(UUID())" to field default , but generate "Sequelize.Sequelize.fn("uuid")", but not work

image

not work mean : When I use /graphql api don't run successful for addTodo

IOLOII avatar Nov 27 '23 17:11 IOLOII