display sql query to create django model object - django

im learning django 1.8.x. In older django(1.4) we can view sql query like
"Begin;
create table "article_article" ("id" integer NOT NULL PRIMARY KEY,
...
...
);
COMMIT"
for creating model object of an APP called article by running :-
python manage.py sql <appname>
is there a way so that i can view sql query used to create model objects for that app in Django 1.8.9 ?
cheers

You can see the SQL per migration now. So assuming it's the first sync (i.e. syncdb in older Django):
python manage.py sqlmigrate <appname> 0001

Related

Django makemigrations show no changes detected after table rename in mysql

I have a Django application with a My-SQL database. recently I alter the table_name with the help of MySQL query in the MySQL-shell, after this when I run makemigration and migrate command terminal says "No changes detected". how can i resolve this issue and create again this table with help of Django makemigration and migrate?
can I delete a table from MySQL, any possibility will Django create it again?
If you renamed your table outside Django - you will have to tell Django the new table name like so (using the Meta class):
class Model(models.Model):
name = models.CharField(max_length=255)
class Meta:
db_table = 'new_table_name'
To re-create your table using existing model you need to reset migration for that app to zero and then run migration again
python manage.py migrate APP_NAME zero
python manage.py migrate APP_NAME
It's because the migrations table managed by django doesn't reflect the correct db schema since it was already modified outside of django. If you don't have any important data you can do a migration rollback or recreate the table by hand.
The best way to dela with this is to rename your table back to the original name. Then create a blank migration inside your app and recreate the sql commands you did in the shell inside that migration file. That way django can keep track of the database schema.
You should change the name of the table in models.py not in MySQL shell.
From
class MyModel(models.Model):
...
To
class ThisModel(models.Model):
...
Or Create Proxy Model :
class ThisModel(MyModel):
class Meta:
proxy = True
verbose_name = "ThisModel"

Change unique=True to unique=False from my model field

I have one field in my model with like this name = models.CharField(max_length=100, unique=True) but now that table/model have a lot of data and need to change the True to False but without having to drop the table and créate it again, How can I do that?
Generate the new migration using:
python manage.py makemigrations
The above will detect changes to your model and generate a migration class but no execute any sql yet.
To generate/apply the sql to the db:
python manage.py migrate
If you want to see the sql that will be executed before updating the db do this before migrate:
python sqlmigrate {app_label} {migration_module}
EDIT: The above will rename your table with suffix __old, create a new table and insert the data from the old to the new one, and then drop the original table. So not sure if this is what you want..
Another option would be to use plain sql to achieve what you want:
ALTER TABLE table_name DROP CONSTRAINT constraint_name;
But remember, in order keep your migrations updated for new runs, find the migration class that declares your field as unique and change unique=True to unique=False. If any other servers need to be updated you can run the drop constraint command there too so everything is in sync.
Just change the value to False in the model and then makemigrations and migrate. This will update all items in the DB to the new value. This is if you are using the newer version with South and not using syncdb.

Migrate new model fields

When I use manage.py makemigrations <app> only columns with relations are migrated to pg database.
How to instruct django to migrate new basic non-relational columns, like:
title = models.CharField(max_length=20, null=True)
I'm using django 1.8.2. on Ubuntu
No fields at all are being migrated when you run manage.py makemigrations <app>, as that command only creates a new migration in your <app>/migrations/ directory. It's only when you run manage.py migrate that changes are written to the database.
If you are not getting the expected results, have a look at the newest migration and determine which fields it affects. All fields should be targeted by migrations, not relational fields only. Changing properties like max_length, blank, required etc. should trigger migrations. If they don't it's probably because your changes doesn't require any database schema modification.
If you are still having problems, please post:
Your models prior to model change
Your models after model change
The migration generated by makemigrations

Syncdb error when creating CustomUser

The issue is that I create Custom User:
class CustomUser(AbstractUser):
mobile = models.CharField(max_length=16)
address = models.CharField(max_length=100)
And in settings.py write this:AUTH_USER_MODEL = 'login.CustomUser'
And when I run manage.py syncdb for the first time(when db doesn't contains any tables), it throws an error: django.db.utils.ProgrammingError: relation "auth_group" does not exist
But when I run manage.py syncdb when db with tables already exist, then it's okay, just create additional tables. What's wrong to run it when db doesn't contain tables?
Try this
Create a migration directory in the directory of the app containing your custom user model.
Create an empty __init__.py file inside the migration directory.
Execute ./manage.py makemigrations.
Execute ./manage.py migrate.
I think when you use a custom user model, you need to create migrations. Besides, syncdb is already deprecated and will be removed in Django 1.9 so it's better to start using the ./manage.py migrate command.
As the error says, Your CustomUser table has relation with auth_group table, so unless that table is created or exists, you cannot create this new table in db.
When you say 'db with tables already exist, then it's okay', did You create the tables manually or included admin app in your INSTALLED_APPS?
Because if u include admin app, it will automatically create the auth_group table, so you will not see that issue

Want to use my existing mysql database with django

I have created a django project and now rendering templates. I have a mysql database already with loads of tables and data.
From what i saw in the tutorials, the model concept of python is interesting and easy however i am not able to use it here, as i have no models available i guess. Was assuming django would magically create models based on my db.
I have filled up settings.py with engine, db, username, host, port etc.,
Do i have to create models based on my tables?
This works thou:
db = MySQLdb.connect(user='root', db='dbBooks', passwd='1234', host='localhost')
cursor = db.cursor()
cursor.execute('SELECT bookname FROM books')
names = [row[0] for row in cursor.fetchall()]
db.close()
return render(request, 'index.html', {'bookNames': names})
inspectdb works fine now. (Django 1.7.1) Simply running manage.py inspectdb will create classes for all tables in database and display on console.
$ python manage.py inspectdb
Save this as a file by using standard Unix output redirection:
$ python manage.py inspectdb > models.py
Reference
There is an way by which Django will auto-magically create models based on tables using the inspectdb option of manage.py. A short guide is provided in Django Documentation itself on Integrating Django with a legacy database