Restriction on Database Schema Commands for Migrated Django Applications
When a Django application contains migration files, certain database schema commands become restricted. Attempting to execute sqlall or similar commands results in the following error:
CommandError: App 'getssh' has migrations. Only the sqlmigrate and sqlflush commands can be used when an app has migrations.
This limitation exists because Django's migration system manages database schema changes explicitly. Running commands like sqlall bypasses this controlled process, potentially causing inconsistencies between the actual database state and the migration history.
To resolve this issue, you may raname or remove the migrations directory within the application folder:
mv migrations/ migrations.back/
After this adjustment, commands such as sql will function correctly, generating the appropriate SQL statements for table creation based on the current model definitions:
BEGIN;
CREATE TABLE `getssh_publisher` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`name` varchar(30) NOT NULL,
`address` varchar(50) NOT NULL,
`city` varchar(60) NOT NULL,
`state_province` varchar(30) NOT NULL,
`country` varchar(50) NOT NULL,
`website` varchar(200) NOT NULL
);
CREATE TABLE `getssh_author` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(40) NOT NULL,
`email` varchar(254) NOT NULL
);
CREATE TABLE `getssh_book_authors` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`book_id` integer NOT NULL,
`author_id` integer NOT NULL,
UNIQUE (`book_id`, `author_id`)
);
ALTER TABLE `getssh_book_authors` ADD CONSTRAINT `author_id_refs_id_20f26b36` FOREIGN KEY (`author_id`) REFERENCES `getssh_author` (`id`);
CREATE TABLE `getssh_book` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`title` varchar(100) NOT NULL,
`publisher_id` integer NOT NULL,
`publication_date` date NOT NULL
);
ALTER TABLE `getssh_book` ADD CONSTRAINT `publisher_id_refs_id_99765d4d` FOREIGN KEY (`publisher_id`) REFERENCES `getssh_publisher` (`id`);
ALTER TABLE `getssh_book_authors` ADD CONSTRAINT `book_id_refs_id_2cfb1c02` FOREIGN KEY (`book_id`) REFERENCES `getssh_book` (`id`);
COMMIT;
This approach allows direct schema generation while maintaining compatibility with Django's migration framework.