I know this might be a sill question but I've been half an hour trying to figure this out and couldn't find anything :S
I have a django app and I significantly changed my database tables. I want to update my models.py file, but I tried the following commands and nothing happens.
syncdb, migrate, makemigrations...
I want to delete my previous models file and create a new one.
Thank you!
You got the workflow the wrong way around.
Django has an ORM that manages your database. If you want to make a change, you edit your models.py file. The migrations will automatically alter the database table to match your new model. It doesn't work the other way around: Django does not use database introspection to pick up manual changes in the database and edit your models file.
Now, there is a workaround, but it's not a long-term solution. In time you'll want to add custom functionality to your models, and you don't want to rewrite that functionality after each change. The introspection Django provides isn't perfect either, it's only meant as a tool to quickly start developing your application on top of a legacy database.
You can use manage.py inspectdb to generate the Django code for all existing tables. You can then copy the code for your specific model over to your models.py file. You should then delete the managed = False and db_table = ... options, remove any migrations, double-check the fields, and rerun makemigrations and migrate --fake-initial. This will get the database, your models, and your migrations back in sync, and then you'll be able to use the migrations framework for any additional changes.
Be sure to read the docs on migrations. That should leave you with a good understanding of how Django manages the database, and what the workflow is to make changes to your database.
Related
I have a database which is used by multiple projects. Each project has its database migration.
I have tried to google and read the knex documentation, but no luck. I have seen some suggestion to fake the migration files to trick the migration table, but I don't think it is a good solution.
I want to keep all the migrations data in one migration table. Is it possible on knex?
Having different set of migration files for each projects and trying to run them separately against the same migration table is not possible. There is no good solution for it and it would not make sense anyways to do it.
If migrations are not related to each other, then there is no reason to have them in the same table. On the other hand if they are related, then the files really should be hosted in the same place to guarantee that everything is done in correct order.
You can setup migration table name tableName (http://knexjs.org/#Migrations-API) to be different for every project in knex config.
However I would never recommend having multiple projects using the same database and everyone having separate migrations for it.
Only reason where that could be remotely acceptable would be the case where you don't have access to create separate databases for each project.
If projects are sharing the same data model (microservices with shared DB), in that case you should still be using multiple databases or to have single service which is the owner of the schema changes and the rest of the services should only read/write data.
I created some tables using rails. Now I want to modify the structure of a few of them. I know it can be done using rails migration. But i was wondering if it would cause any anomaly in the rails app if I modify the schemas using mysql rdbms?
Doing such changes through a migration has the advantage of not losing the changes if you decide to recreate/remigrate the database.
Also it serves as documentation. Imagine if your coworker altered some tables sneakily (and then you both forgot about it).
Technically, updating schemas directly in the database should work, but don't do it.
To add to Sergio's point, you're missing a simple fact - Rails' migrations create the famous db/schema.rb file - from which your migrations pull all their data.
The importance of schema.rb is overlooked - it is one of the most crucial aspects of your application.
db/schema.rb
The schema gives all your migrations a version of your DB to change / add to. Each time you perform a migration, Rails changes the schema file to ensure it has a "blueprint" of your db stored on file.
The schema is then able to rebuild the database using such methods as rake db:schema:load (ONLY RECOMMENDED FOR NEW INSTALLS -- DELETES PREVIOUS DATA)
So whilst there's no problem setting up the db using the db's own native tools, I recommend against it. You need to keep your migrations up to speed so that Rails can build the appropriate tables from its schema.
Hi I am newbie to django development, but anyway I will try in this question do not to be generall.
I have strange problems while designing my model. I want to make a lot of changes - like alwyas in the begining of the project. The problem is that after one syncdb I can not change anything in model, unless I create completly new database and link it in settings.py. What is pretty strange I even can not delete this old database. When I try do it from Sequel Pro it freezes.
I googled a bit, and is really South or similar tool the only option to have some fredoom in models desgining phase in django?
I know that this mechanisms are for certain purpose like keeping data consistency on existing project, but I just started my development and have even 0 records in all the tables.
I user in my development 5.6.13 MySQL Community Server, Django 1.5. All runing on OS X.
South is a great tool for migration or if you have made some changes. It may take a little time to get through for the beginners as it was for me too, but eventually you will like it. Other than that as per your question, you can use flush, which replaced reset in django 1.5. And please remember, that it flush all the database and not just the app.
python manage.py flush
But luckily reset has been ported back in here. All you have to do is install, and add it in the settings. And then run
python manage.py reset appname
Hope this helped!
OK. Partially I found answer here: Django: Change models without clearing all data?
SAYING: "syncdb will only create tables for models which have not yet been installed. It will never issue ALTER TABLE statements to match changes made to a model class after installation. Changes to model classes and database schemas often involve some form of ambiguity and, in those cases, Django would have to guess at the correct changes to make. There is a risk that critical data would be lost in the process. If you have made changes to a model and wish to alter the database tables to match, use the sql command to display the new SQL structure and compare that to your existing table schema to work out the changes."
OK. I see that I can run python manage.py sql <app_name> then should I paste it in python manage.py dbshell?
via dbshell I managed to delete whole database, what was not possible in Sequel Pro.
python manage.py dbshell
and then via MySQL:
drop database <db_name>;
And why the database when accesing from Sequel Pro is froozen?
I was trying to avoid South, because there were many opinions that it is confusing, and time consuming to learn. After sometime I look back on SOUTH and I have to say:
It is completely WORTH learning it, what is not hard at all.
It is very nice tool for dealing with model, which changes dynamically.
I can propose a nice tutorial video to all newbies, who will consider either it is or not worth to learn. http://www.youtube.com/watch?v=7yCmAhthkMk
So in my company we are slowly moving to Rails instead of PHP(Code Igniter to be precise).
So, our actual PHP App is using a Mysql DB and I'd like to connect a new Rails app to this DB but meanwhile our PHP is still running, so I can't change the DB.
I don't really know where I should start to use all the rails features (Or at least as much as possible).
There shouldn't be any harm in connecting your rails app to an existing database. You will need to watch for anything that goes against rails conventions (table names are plurals of models, for example) and either change the database (and your php app) or program around the problem in rails.
But the first step is simply to connect to the database and make models for the existing tables and see what works and what doesn't.
After that, post here with any specific problems.
As a suggestion, take a backup of your database and start out programming against that to build your application and be sure everything works safely.
Well, first of all you should setup the connection in config/database.yml and then start to generate the scaffolding (models, views and controllers) table by table (check the Rails generate command). I am not really sure if you have already generated the app though. Anyway, the generator will also generate a migration script that you obviously dont want to run as the db is already there.
Hope this helps a bit.
Anyway, some resources:
http://guides.rubyonrails.org/
http://railsapps.github.io/
There are two aspects of a Rails app to consider for this scenario:
1: the database connection
Simply put the credentials for this database into database.yml.
A model like "User" will by default attempt to find records and attribute definitions in a table called "users". ActiveRecord will assume there's an auto-incrementing integer primary key on each table. When saving records, it will attempt to write to columns called created_at and updated_at. Those are a few things to be mindful of when making and using the connection.
2: the database migrations
Rails uses migration files to manage a sequence of changes to the database structure. Under normal conditions, someone building a Rails app will be starting with an empty database.
In the case of an existing database, I would recommend making a migration something like:
class BuildLegacyDbStructure < ActiveRecord::Migration
def up
Mysql2.connection.execute_some_sql_file( # made-up function
Rails.root.join('path', 'to', 'file')
)
end
def down
# reverse those changes; bring DB down to blank state
end
end
Another option would be to disable Rails/ActiveRecord's migration-based management of the database entirely. For example Rails will generate a migration when you generate a new model. So if you have an existing users table in your PHP app, and you'd like to make a rails model to use this table, you'd run something like rails generate model User --no-migration.
I have an application written in PHP/MySQL (symfony, to be specific) that I'd (potentially) like to rewrite in Rails. I know how to create scaffolding for tables that don't exist yet, but how do I get Rails to read my existing table structure and create scaffolding based on that?
Update: it turns out I can run the following command to get Rails to generate models for me:
rails generate scaffold Bank --no-migration
But it doesn't give me forms. I would prefer something that gives me forms.
The answer is db:schema:dump.
http://guides.rubyonrails.org/migrations.html
The easiest route is to pretend that you are writing a fresh app with a similar database schema - you can then create your models and migrations with the old schema in mind, but without being restricted by it. At a later stage, you can create a database migration script to copy all the old data into the new schema.
I'm doing this right now. The benefit of this approach is that you can take advantage of all of the rapid development tools and techniques provided by Rails (including scaffolds) without being slowed by trying to retrofit to the exact same schema.
However, if you do decide that you don't like this approach, and you do need to map your new models to existing tables, there are a number of configuration options provided by active record where you can override the convention over configuration naming patterns and map model names to tables names, set oddly named ID fields etc. For example:
class Mammals < ActiveRecord::Base
set_table_name "tbl_Squirrels"
set_primary_key :squirrel_id
end
The above will help Rails attempt to read your existing table, but success will depend upon how well the existing table structures matches Rails conventions. You may have to supply more configuration information to get it to work, and even then it might not work.
Finally, it may be worth considering the use of DataMapper which I believe is more suited to existing brownfield databases than ActiveRecord, because it allows you to map everything, but of course you will need to learn that API if you don't already know it.