I have a table named Email in models.py. I want to add additional columns to it. I have tried adding the additional column to the models.py file, saving it, and then doing a
$ python manage.py syncdb,
but it is not updating the table columns (I imagine because it recognizes that the table already exists in the database and skips over it).
How do I update a table that already exists in django?
syncdb creates tables if the table does not already exist. Any alterations in table that already exists is not handled by syncdb. Either you have to manually alter the tables or use a migration tool like south.
Django does not support automatic changes to the schema. See the Django book chapter on models, in particular the section titled "Making Changes to a Database Schema". Some third-party support for database migration is available.
Related
I have two databases. Now I'm trying to import all table schema only from first database to second database. I already exported all table structure from first database using phpmyadmin. But I can't import the table structures in the second database. phpmyadmin says
#1050 - Table 'XXXXXX' already exists
How can I import only the table structure correctly?
Note: Both databases had same table and all table had same structure. I have changed some table structures in the first database that I can't remember right now. Now I need to merge both table structure only. Also both database contains different data set and I can't afford any data loss from both databases.
Before executing any command I would recommend taking full database backup, otherwise you may lost a few days of sleep.
Using ALTER command
Use ALTER command to modify table structure. Here's sql that adds age not nullable age field to users table.
ALTER TABLE users ADD age int(11) not null
Re-creating table
I wouldn't recommend this method because you'll have data loss. Drop old table then create with new schema.
DROP TABLE mytable;
CREATE TABLE mytable (
...
);
Or if you want to keep data you can:
Duplicate or rename table to different name.
Create a new table with new schema.
Copy data from old table: INSERT INTO newtable SELECT * FROM oldtable
Renaming tables might cause relationship issues. I would recommend using ALTER command as much as possible. You can also take a look at scheme migration(aka: code first migration, database migration).
The main Issue is merging the tables. To identify the differences between the two tables I use a small software called SQL Power Architect.
I have a mysql table y in database xxx which I attempted to change compression type before using
alter table y row_format=compressed key_block_size=8
the process stopped half way. I removed temp file '#sql-ib265.frm and #sql-ib265' in mysql lib directory and restarted the server. However
Now when I attempt the alter table y (with the same command above) again I get error.
ERROR 1050 (42S01) at line 1: Table 'xxx/#sql-ib265' already exists
I can't drop table 'xxx/#sql-ib265' because it can't be found.
what should I do?
Edit
Solution:
I ended up dropping the old database and recreate the database.
Try to restart mysql client with the --skip-auto-rehash option and try DROP TABLE again.
If above does not work, try this from MySQL Manual:
You have a corrupt innodb data dictionary..
https://dev.mysql.com/doc/refman/5.0/en/innodb-troubleshooting-datadict.html
Problem with Temporary Table
If MySQL crashes in the middle of an ALTER TABLE operation, you may end up with an orphaned temporary table inside the InnoDB tablespace. Using the Table Monitor, you can see listed a table with a name that begins with #sql-. You can perform SQL statements on tables whose name contains the character “#” if you enclose the name within backticks. Thus, you can drop such an orphaned table like any other orphaned table using the method described earlier. To copy or rename a file in the Unix shell, you need to put the file name in double quotation marks if the file name contains “#”.
There are two ways to fix this problem.
As other answer suggests, official MySQL documentation suggests to drop a specially crafted table. But please note in versions >= 5.1 you need to prefix table name with #mysql50#.
Move (use RENAME TO) all good tables to a temporary database, drop&recreate the original one and then move the tables back. See a blog post for details.
in additional I'm loging in with root to do the recover job but failed. then i chown the .frm file to meet the owner of mysql service and succeed.
For anyone still facing this problem, I have just followed the following steps to solve it, which (to me at least) seem far less daunting than other solutions:
Use mysqldump to back up the database with all its data.
Drop and recreate the database.
Reload the database and all its schema from the file generated in (1).
Because the orphaned tables are hidden anyway, they don't get backed up, so you end up with a database without them. I had all my procedures/functions scripted out anyway, so was able to restore them easily - if you don't, make sure you use the --routines parameter to dump those too.
My dump file was around 1.5GB for the database in question (so it's not small), and the whole thing was completed in a few minutes.
I had the same error. I fixed it by switching the order in which I dropped the tables at the beginning of the file:
DROP TABLE IF EXISTS table_name;
This line is repeated for each table. Tables with foreign keys need to be deleted before the tables with the primary keys to which they point.
I need to rename a Django Model in the app "myapp" from "Hotel" to "Client" and I would prefer not to use South.
I am wondering it safe to handle the changes in MySQL using queries such as the following?
RENAME TABLE myapp_hotel TO myapp_client;
RENAME TABLE myapp_hotel_sites TO myapp_client_sites;
and for tables with a Foreign Key relationship to the now Client table:
ALTER TABLE myapp_client_sites CHANGE hotel_id client_id int(11);
Can it be as simple as that, or am I missing something?
Changing the table name can be done like this in MYSQL, but take into consideration you will also need to change the class names within the django code also. Else the orm will not map with the mysql database table names.
Using a tool like south will ensure all the code changes are made where required within the database.
Automatic migration creation: South can see what’s changed in your models.py file and automatically write migrations that match your changes.
Is it possible to alter the schema of a mysql table by simply providing the new schema and having the database figure out how to migrate? For example, let's say I have a table with two columns: id, name. I want to modify this table by adding a new column: title. I know that I can issue the command ALTER TABLE tbl ADD COLUMN title. Is there a way I can just provide the complete schema and have mysql figure out that it needs to add a title column?
I hope that makes sense what I am asking.
MySQL can't do this by itself, but I found a blog that says MySQL Workbench can do it.
No - It cannot do that just giving your database a new schema. It would have to go about and second guess what to do with the data. (Also what happens to triggers, stored procedures ...)
You have a couple of choices
Modify each table and decide what needs to be done
Export the data, create a new database and then figure out how to import it into the new regime.
I'm working with Play! 1.2.4 and I've come across a curious issue.
As far as I'm aware if I set the jpa.ddl in my configuration to create-drop it should drop my tables and rebuild and application restart.
jpa.ddl=create-drop
Am I right in thinking that it will only drop and create tables associated with models that have changed? I'm getting a problem where I have a model which has changed, but it isn't dropping the table. I tried to drop the table manually but it won't allow it because it Cannot delete or update a parent row: a foreign key constraint fails. I understand this problem and to fix it I could manually drop my entire table and restart my application so it builds the tables from scratch.
My question is, is this the problem that Play! is having which is why it isn't updating that table, and if so is there a way to get around it through configuration files rather than manually dropping my table?
Thanks.
EDIT
Just for some more information, I am just assuming this is a problem and it might be something completely different but here is what I get in my logs:
Unsuccessful: create table Product
Table 'Product' already exists
I also just realised a change occuring on this load. I used to have a relationship like so
Product *-* Image
That being a ManyToMany relationship between the Product and Image table. The Image table is now not there and the relationship will be gone. However, it looks to me like the Image table isn't being deleted but the Product one is trying to be deleted and rebuilt. This might be causing the issue wit the foreign key constraint. Why wouldn't Play delete that table if its Model doesn't exist anymore?
Runing play in dev mode?
But why don't you use jpa.ddl=update?
Automatic schema modification with jpa is not really the best way to do. Play provides the evolutions mechanism which is more reliable because you indicate what are the changes.
In the example you give, if you delete a model class like Image, JPA does not know anything about Image class anymore so it won't delete the Image table and the Product_Image relationshop table. Thus it can't delete the Product table. JPA do not have any knowledge on what was the database before you change your model.
Evolutions are a bit more tedious to do use at start because you create evolutions files by hand but with this mechanism, your database structure is exactly what you want