On Thu, May 12, 2016 at 11:46:35PM -0700, schaf...@gmail.com wrote: > HI All, > I'm new in south and I'm wondering how I can merge two models into one.
Hi Marcel, If you are using South, that would mean you're using a version of Django that's oldee than 1.7, which means you are using a version that hasn't received security updates for more than a year. Not good. Second, South itself has been deprecated and replaced by the native django.db.migrations. Learning to use South at this point is not the best idea, because while the native migrations framework is based on the same ideas, it works differently in some ways. That being said, let's look at what you are doing... > I have a model book which derives from the model media. > So book has a media_id_ptr (which is already a pk). > > No I would like to merge all attributes from media into book and get rid of > media. I would suggest that you do this the other way around; instead of merging Media into Book, I'd merge Book into Media, drop Book, and rename Media to Book. Since you're using multi-table inheritance, the child model's primary key needs to be its OneToOneField pointing to the parent, so you'd need to alter a OneToOneField into an auto-incrementing ID field. While it may be possible to do that, it's not something I'd want to try myself. There are many potential pitfalls in that, and each database appears to have a unique way of handling auto-incrementing ID columns, each with its own set of limitations. > I was doing the following: > > 1.) Added all media attributes in the book model, but with _tmp ending. > 2.) Created a schemamigration (also creates a correlation table with ending > _tmp) > 3.) Created a datamigration and copied the attribute value into the > attribute_tmp for all existing book objects. > 4.) modified the book model to get rid of the _tmp ending. > 5.) Created a schemamigration and exchanged (delete/add with rename column) > additionally I would like to rename media_id_ptr into id and use it as > primary key. > > But it always fails when I would like to rename the media_id_ptr into id. > I have different tables (m2m and normal foreign keys) which point to > media_id_ptr. > > Therefore I have 2 question: > 1.) I guess it is not that easy to reuse the former foreign key as a > primary key. Is there a best practice to handle that? DO I have to remove > the foreign key and add it after renaming for all tables which referenced > it before? If you have other tables referencing a field that you want to alter, that can complicate things a *lot*. This depends on the database you are using – some databases will automatically cascade certain column alterations to any constraints that reference them, others will just error out and leave it up to you to sort this out. If you want your application to work on multiple databases, I'm afraid you'll have to drop any FOREIGN KEY constraints referencing your column, make the change, and recreate them with the new target name. > 2.) Best practice to rename the m2m_tmp table DO I have to delete the > unique keys and re-add them after renaming? I don't think it's necessary to drop UNIQUE constraints in a table before renaming it, but I may be wrong (and it might also depend on the database you are using). Are you getting any errors when you try to do this? Good luck, Michal -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20160513080250.GE435%40koniiiik.org. For more options, visit https://groups.google.com/d/optout.
signature.asc
Description: Digital signature