Cli: Generate initial migration from model?

Created on 21 Jul 2015  ·  202Comments  ·  Source: sequelize/cli

I'm just getting started with Sequelize and the CLI. I want my project's initial database to be bootstrapable with sequelize db:migrate (i.e. create the database from nothing with an initial set of migrations). In that regard, it seems to me that I am duplicating a lot of code.

  1. I create my initial model, wherein I fully describe the table
  2. I then create a migration for it where I mostly just copy and paste the previously written definition
  3. If there are changes to be made before committing, I get to make them in multiple places

I have a simple two table test project at https://github.com/jsumners/sequelize-migration-test that shows what I'm describing.

Would it not be possible (make more sense) for sequelize migration:generate to read the already defined model and generate the migration from that definition?

Most helpful comment

Yes. That would be a good idea and I'm planning to do that since a while already but never found the time for that :(

On Tue, Jul 21, 2015 at 11:09 PM, James Sumners [email protected]
wrote:

I'm just getting started with Sequelize and the CLI. I want my project's initial database to be bootstrapable with sequelize db:migrate (i.e. create the database from nothing with an initial set of migrations). In that regard, it seems to me that I am duplicating a lot of code.

  1. I create my initial model, wherein I fully describe the table
  2. I then create a migration for it where I mostly just copy and paste the previously written definition
  3. If there are changes to be made before committing, I get to make them in multiple places
    I have a simple two table test project at https://github.com/jsumners/sequelize-migration-test that shows what I'm describing.

Would it not be possible (make more sense) for sequelize migration:generate to read the already defined model and generate the migration from that definition?

Reply to this email directly or view it on GitHub:
https://github.com/sequelize/cli/issues/157

All 202 comments

Yes. That would be a good idea and I'm planning to do that since a while already but never found the time for that :(

On Tue, Jul 21, 2015 at 11:09 PM, James Sumners [email protected]
wrote:

I'm just getting started with Sequelize and the CLI. I want my project's initial database to be bootstrapable with sequelize db:migrate (i.e. create the database from nothing with an initial set of migrations). In that regard, it seems to me that I am duplicating a lot of code.

  1. I create my initial model, wherein I fully describe the table
  2. I then create a migration for it where I mostly just copy and paste the previously written definition
  3. If there are changes to be made before committing, I get to make them in multiple places
    I have a simple two table test project at https://github.com/jsumners/sequelize-migration-test that shows what I'm describing.

Would it not be possible (make more sense) for sequelize migration:generate to read the already defined model and generate the migration from that definition?

Reply to this email directly or view it on GitHub:
https://github.com/sequelize/cli/issues/157

If I get a chance I'll see if I can work something up. But I'm not sure I'll have the opportunity.

+1 :)

+1

+1. Well, it is just javascript, so im going to make a file that exports the javascript object, that the model in models/ and migrations/ can import. Not going to set my self up for the inevitable 'change one place, but forgot the other'

+1

+1

Is there any progress? or alternative?

no progress afaik

+1 Will be the very popular feature. I hope this feature will be implemented

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

By the way, now I use https://github.com/Vincit/objection.js/
Very nice convenient ORM

+1

Though currently I'm doing it this way: https://gist.github.com/notgiorgi/7276aae5baebb5725799ce0f5b8468f3

I would like to start working on this feature. Can anyone give me pointers on where I should begin?
Thanks a lot.

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1

+1 this would be a huge help.

+1

+1

Is this still open ? It would be awesome to be able to generate migration from already existing/edited model

+1

+1

+1 This is actually the way migrations should work. A great reference for the mechanics of migrations is Microsoft's Entity Framework Core.

Essentially, you're free to write your model using EF Core and run migrations from the dotnet-cli. Your entities and their relationships are then read and parsed by EF Core and the entire model is setup for commit to a new (or existing) database.

Was hoping to see this awesome feature in Sequelize v4. :(

+1

+1 and is there any update or schedule for this issue? @sdepold

+1

+1

+1

+1

+1

+1

+1

+100000000 Django makes this work fantastic!. If Sequelize could make it the same way, wuaaaa it would be great!

  • Look for a way to do something
  • Find an issue describing exactly what you want to do
  • Check date: 2 years ago
  • Sweet, must be a way to do this by now! :)
  • Scroll down
  • Nope, still nothing
  • Sadness :(

Since this issue continues to get comments...

I ended up writing https://www.npmjs.com/package/cloob to suit my needs.

+1

this has already been done in the new version of sequelize

@bradleyy1012 Can you show an example how to do it please?

I might have meant the new version of sequelize-cli but basically u just create the model from the cli and it creates the migration for you too.

@bradleyy1012 But isn't this proposal about something different? I mean generating migration from a hand-written model like Code First Migrations in Entity Framework.

ahh i see what you mean. i misunderstood cause its been a while since i visited this post. yea theres still no way to do that yet that i know of :(

:+1: This is a pretty core feature in most ORM/migration tools that are as fully featured as sequelize

Ok I’ll just try to write it myself and hope for an eventual integration into sequelize-cli. @sdepold any pointers?

The project will probably be based on this blog post using SQL dump files and Sequelize

+1

duplicate on #120 ?

duplicate on #120 ?

No, that issue was about camelCase vs underscored column names.

+1

+1

+1

+1

1f44d

+1

+1

+1

+1

+1

Found this gist

+1

+1, with the ability to "generate migration from model", not only initial.

Current state is "generate migration along with model"

+1 I really like Sequelize. This would make it so much better.

+1

Please stop +1 postings. It helps nobody and notifies everybody watching this thread. I would recommend using the emojis instead to express yourself. Thank you.

Edit: I'm not gonna argue on that, but there was a reason why +1 Emoji was introduced: https://github.com/isaacs/github/issues/9

The whole point of writing "+1" is accurately to notify every subscribed people.

A "thumb up emoji" 1 milion times is less valuable than 10 "+1", for the same reason.

Feel free to unregister if you don't want to be notified, and let us promote this 3-years-old non addressed feature. Noone is responsible for you not unsubscribing from issues, configuring issues or your email client.

+1 definately

EDIT: plus you got notified because you suggested a "mark as duplicate"

duplicate on #120 ?

Without even reading related issue

duplicate on #120 ?
No, that issue was about camelCase vs underscored column names.

Do you find yourself generating 3 pointless notifications, more useful than people voting for a feature ?

It's also fair to point out issues get Closed after some time, regardless if they are still relevant. The bot does not take emojis into account afaik.

+1

+1

+1

+1

+1

+1, again

+1

First I created a model with model:generate whereby I added 5 columns.
Then I extended the model by hand to 15 columns and copied same code to the migration file.
Still although Sequelize prints 'CREATE TABLE IF NOT EXIST' for the model in question at each start, and correctly describes 15 columns, no additional columns were created in the database.
Is there a way to do what I wanted to, which is to link db table creation to hand-written model, or not? Do I have to create 15 columns table anew with model:generate?

Thanks!
+1

seriously? +1000

+1

+1

+1

:+1:

+1

+1

+1

+1

+1

+1

+1

+1... Come on.

+1

+1

+1

+1

+1 I'd really love to see this feature.

+100

screen shot 2018-05-03 at 3 50 05 pm

I'll just leave this here

+100

+1

+1

+1

+1

+1

+1

I am gonna try this (https://www.npmjs.com/package/sequelize-auto-migrations), but would love to see this as a core feature.

+1

it has been ~3 years since, no solution yet?

+1

+1

+1

+1

+2

+1

+1 👍

+1

+1

+1

+1

+1

+1

😭😭😭

+1

+1

+1

+1

+1

+1

+1

+1

Any updates on this one?

+1

+1

+1

+1

+2

+1

+1

+1

I'll take this on if no one else has. I need this right now for my work so it makes sense.

I'll take this on if no one else has. I need this right now for my work so it makes sense.

@everettcaleb I'd be happy to help with it. I used this tool sequelize-auto-migrations (although, I had to mod the npm package locally to get it to work on my windows pc) which may be a good starting point. I used it before I wrote a migration, and now I have written many by hand and feel confident helping define what each seuqelize command should output and building it.

I'll email the address on your profile.

I'll take this on

OMFG @everettcaleb I think 200 developers would owe you a coffee if you did so.

if no one else has

Trust me I've been following this topic 2 years ago, no one is and no one has never been 😅

EDIT: can help if you drive me a little.

@TimAConner and @cyrilchapon email me and we can collaborate

@everettcaleb Just emailed you!

+1

(+1 —though isn't that what the 👍 reaction on the issue is for? 😉)

For a project I'm working on, I went with a poor man's solution by:

  1. dumping the models as JSON (the .name, .attributes and .options property of each model, plus fixing the attributes to retain their type information)
  2. then stored the .json file as a snapshot of the initial schema in a migration-extras/ directory
  3. made my first migration script use the .json file to re-instantiate the model definitions and make calls to .sync() each of them to the DB.

Here's a gist I made of what I did if it would help anyone before this issue is officially solved: https://gist.github.com/scryptonite/3bee4b3d55485230db113af639a0601f

I think this feature is not only useful for convenience but it would also act a better primer/starting point for those new sequelize in order to help understand the best practice for working with sync/define/migrations

+1

+1

I have written a utility to create initial migrations from existing models, I think I can add this feature to the cli.

Can I take this issue? @sushantdhiman

+1

+1.

+1

+1...

+1

+1

+1

+1

+1

4 years passed ...

+1

+1

+1

+1

+1

+1, hello from 2019

The +1's aren't doing anything to promote this issue. It's probably only contributed to lessening the visibility of this issue for the project maintainers—they've probably muted this issue and have now almost completely forgotten about it. Someone either just needs to work to submit a PR to fix this issue, or just wait patiently for the maintainers to get around to it. I would prefer not to have to mute this thread for myself, as I too earnestly wait for an official solution to this issue. I just ask that everyone would stop commenting with merely +1 and nothing else to add to the discussion. 😅

For the impatient, I've already commented above that I came up with a poor man's fix for folks who (like me) rushed ahead with their own projects by defining models but didn't bother writing the setup migrations for them. https://github.com/sequelize/cli/issues/157#issuecomment-423715290
I've used the same-ish script I put in that gist for several personal side-projects of mine and it's proven adequate for the helping with creating the initial starting-point migration.

If my gist/outlined steps don't fit your particular project's needs, maybe comment about it so that whoever is finally brave enough to submit a PR for this issue, can possibly account for your edge case(s).

The developers and maintainers have done a great job at building this library, needless to say I have never enjoyed working with SQL more when it comes it sequelize. A couple of commands and whoosh Magic and the interface is pretty amazing too.

When I started with a model and couldn't write all the fields and wrote just one to create migration and model file. Once I was finished with the model and thought of synchronizing the two, I ended up here. I hope this get fixed soon.

+1

Has any work been done towards achieving this? Can anyone outline the steps that should be taken to do this?

I come from Django which does all of this for you, so it was very surprisingly to learn that such basic ORM functionality does not exist out of the box for this package (same with: https://github.com/sequelize/cli/issues/257)

Our company is starting a new long term project using Sequelize. Since this is obviously very sought after functionality based on all the +1s; and since the maintainers are absent in addressing it, if someone can point me to any initial work that has already been done on the subject we can start to add some cycles to addressing it.

From what I see, these are the best candidates for this type of functionality so far:

Instead of all the people saying +1 why don't we just do something about it?

@jsumners @sdepold have you made any progress, or do you have an outline of the steps you would take to add this feature?

If a PR was made to add this, would it be addressed or is this something best left to external packages like sequelize-auto-migrations

@cjancsar sorry. I never progressed past reasearching this module.

I had started reverse engineering the django orm a month ago. The important
thing is there is no diagram or such algorithm that atleast outlines the
necessary steps.
My analysis:-

  1. Django looks for a migrations folder.
  2. If present it searches for the latest migration.
  3. It compares the current change with migration file along with the
    database fields i.e. it identifies changes as operations.
  4. Creates a string out of these operations and then writes it to a file.
  1. It also writes dependencies to the newly created file in order to
    rollback changes. These are done by a singly linked list and if any
    dependency loop occurs an error is thrown.

Someone please let me know if any tutorials exist for creating this. We can
split the tasks amongst ourselves. This can be completed in 2 months time
with adequate research.

Karra Raghu Ram

On Sun, 7 Jul, 2019, 9:25 PM James Sumners, notifications@github.com
wrote:

@cjancsar https://github.com/cjancsar sorry. I never progressed past
reasearching this module.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/sequelize/cli/issues/157?email_source=notifications&email_token=AD34HHEGJH2C724VU5SLF6LP6IGWPA5CNFSM4BLUMXD2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZLOGQA#issuecomment-509010752,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AD34HHBYZBIU7L47VZG5MN3P6IGWPANCNFSM4BLUMXDQ
.

+1

Started using sequelizer today, seems a great ORM but just jumped into this, which I think is a feature that it should have from the beggining :(

Started using sequelizer today, seems a great ORM but just jumped into this, which I think is a feature that it should have from the beggining :(

Same!

I am literally going to abandon sequelize, never use it again, and recommend everyone else stay far away from it if this feature doesn't come very soon.

Guys, I managed to get myself another tool working next to sequelize, and now I have the full set. I use Sequelize only as a mapper for interacting with the database, and for migrations Im using Postgrator. I really recommend you guys taking a look at it. It's really similar to Flyway (for Spring) and It applies database version control in an easy way. I find It a great combo, and I dont need the migrations features of Sequelize anymore, I even find that Postgrator is better with that than Sequelize would be.

So just create the DB with the Sequelize models, and when you are ready just dump it and use that .sql file as the first version of your database for Postgrator :)

No progress yet ?

还没有进展吗?
+1

+1

That's a very bad feeling......

This feature should really be implemented in the official CLI. There was a nice side project doing it but it feels a bit abandon :

https://github.com/flexxnn/sequelize-auto-migrations

Might still be a good pointer to start the implementation.

@janmeier this feature request is opened since 2015. Could you let us know if there is any progress on it ?

To me, sync is most of the time not a good idea.
And migrations is the right path to go but it is painful. So auto-generate migrations from models would be really nice

+1, hello from 2020

Any hope in 2020?

No hope :))

@webdevbyjoss as I commented previously, sadly this project feels a bit abandoned ...

Hi everyone, this project is not abandoned, however I am super busy at the main repo for now; hopefully in the near future I will be able to make the super overhaul that I am planning for the Sequelize CLI. Thanks for the patience so far :grimacing:

Hi @papb, I was speaking about https://github.com/flexxnn/sequelize-auto-migrations.
But I'm really glad that you plan to take a deeper look at this issue in the near futur.

I wonder if anyone could recommend an alternative library which has this feature implemented.

Hi everyone, this project is not abandoned, however I am super busy at the main repo for now; hopefully in the near future I will be able to make the super overhaul that I am planning for the Sequelize CLI. Thanks for the patience so far 😬

define "near"

i see this is a miss from sequelize, it can be every helpful generating initial migration from defined models, here we have to copy it again in two places with sync off feature !!

Was this page helpful?
0 / 5 - 0 ratings