How to update a Postgres schema with flask-sqlalchemy? - python

I'm running a simple Flask app with Heroku, and I can run the following command to create all the tables:
db.create_all()
However, I ship new stuff frequently and I often need to add new columns to existing tables. Is there an easy way to do this?
If I need to manually create a new column with Postgres, how would I access the repl for Heroku's Postgres database?

You should be using migrations.
This is a great plugin for that: https://flask-migrate.readthedocs.io/en/latest/
and this is a good explanation about how to get going with that: https://realpython.com/flask-by-example-part-2-postgres-sqlalchemy-and-alembic/

Related

How to use existing mysql DB along with its migrations in sql, in the Django? What changes will need to do in the django for using the mysql db?

I am already having one mysql database with a lot of data, of which tables and migrations are written in sql. Now I want to use the same mysql database in django so that I can use the data in that database.I am expecting that there will not be need for making the migrations as I am not going to write the models in Django again, also what will be the changes/modification I will have to do. For eg: as in middlewares?. Can anyone please help me in this?
From what I know there is no 100 % automatic way to achieve that.
You can use the following command
python manage.py inspectdb
It will generate a list of unmanaged models that you can export to a model.py file and integrate in your django project.
However it is not magical and there are a lot of edge cases so the generated list of model should be manually inspected before being integrated.
More info here : https://docs.djangoproject.com/en/3.0/ref/django-admin/#django-admin-inspectdb

How to have PyCharm show the tables in a Django SQLite database?

I'm not very familiar with Django (I've used more Flask, Web.py, and Falcon), and one thing that I'm finding strange is that when I look at the 'Database' tab in PyCharm, I don't see a list of all of the tables that seem to be getting used in the database:
If I use python manage.py dbshell to start a sqlite3 session and then type .tables, this is the list of tables that I see:
How do I get those tables to be browsable from within the PyCharm database viewer?
Posting the comment as an answer:
PyCharm is occasionally a little slow on the uptake for new tables.
The Refresh/Synchronize button on the Database panel usually fixes that.

I am using Peewee and I need to create migration file for my database

How should I start here, I am a bit confused.
I have database schemas which are newer than the old database.
I have seen Arnold package, where there are two methods arnold up and arnold down. There are migration files where you can add all your database queries. But still those changes I can see and I can add in up like create table or alter table and in arnold down I can add drop table or alter table. But my migration will become only for that database.
I want to understand what does database migration should contain. And what does it do. I will be very grateful if someone explain me database migration and push me in the right direction of peewee, psql database migration
We use a tool peewee-db-evolve that I think you'll find useful. Try this:
sudo pip install peewee-db-evolve
Add import peeweedbevolve to the top of your models.py file, or anywhere that is imported before the models are defined.
Open up your shell and run db.evolve() on your peewee database object.
For (3), at work we have a script evolve.py that looks like this:
import peeweedbevolve
from config import db
if __name__=='__main__': db.evolve()
Just to make it easy.
It will look at the models and your existing schema and calculate all the ALTER TABLE statements for you. (No need to manually write the migrations as w/ arnold, or peewee's built in version.)
It'll look like this:
Hope this helps!

Django 1.8 and Python 2.7 using PostgreSQL DB help in fetching

I'm making an application that will fetch data from a/n (external) postgreSQL database with multiple tables.
Any idea how I can use inspectdb only on a SINGLE table? (I only need that table)
Also, the data in the database would by changing continuously. How do I manage that? Do I have to continuously run inspectdb? But what will happen to junk values then?
I think you have misunderstood what inspectdb does. It creates a model for an existing database table. It doesn't copy or replicate that table; it simply allows Django to talk to that table, exactly as it talks to any other table. There's no copying or auto-fetching of data; the data stays where it is, and Django reads it as normal.

Different Postgres users for syncdb/migrations and general database access in Django

I'm using Django 1.6 with PostgreSQL and I want to use a two different Postgres users - one for creating the initial tables (syncdb) and performing migrations, and one for general access to the database in my application. Is there a way of doing this?
From ./manage.py help syncdb:
--database=DATABASE Nominates a database to synchronize. Defaults to the
"default" database.
You can add another database definition in your DATABASES configuration, and run ./manage.py syncdb --database=name_of_database_definition. You might want to create a small wrapper script for running that command, so that you don't have to type out the --database=... parameter by hand every time.
south also supports that option, so you can also use it to specify the database for your migrations.

Categories

Resources