Accessing DB create by Django using psql - django

I am using Django (version 1.8.2) along with PostgreSql (v 9.4.4) on python (v2.7.9 - anaconda distribution) to create an application.
I am creating tables in database by defining them in models.py of Django. Once, I have created these tables I am able to view and add content of these tables in the browser (http://127.0.0.1:8000/admin/).
However, when I try to access these tables using psql, I get the error as relation xyz_abc does not exist, where xyz_abc is a table present in database as seen by using \dt.
I am not able to understand why this should be the case and any suggestions on how this tables can be accessed using psql will be really helpful.
Edit
Steps to access the database are -
Open the terminal in max
type 'pqsl' in terminal which opens the psql
\c testdb - this connects to the database which contains the table xyz_abc
once connected the, i try to select everything from the table using select * from xyz_abc;

Related

Connect django to legacy postgresql database, already exist and have 1 table

am trying to connect postgresql database with a table already created (legacy).
Not much documentation on the Internet to do so as the database already created. Am afraid to lose the data. Any guide to do that.
Am using Windows and gitbash and have pgadmin4.
Still learning.
Default database in settings file changed to my database but what's next migrate or inspectdb or create tables in django?!
Any help is appreciated.

problem when restoring postgresql database for a django application

I am trying to transfer my testing server's database to my working PC. So I do this:
(Postgres 12.5)
pg_dump -U nutrition -d mednutrition_db --clean -f mednut_bak.sql
and then I try to restore it like this on my Postgres 9.5
psql -U chris mednutrition_db < mednut_bak.sql
This simple method was working fine for me. But now it seems there is a problem which I cannot solve.
I noticed that postgres creates (django)ID fields using this default value (for example):
nextval('django_migrations_id_seq'::regclass)
However, when I restore the database, the default value for the ID fields remains empty, and of course I cannot insert any new data to the restored database.
How can I resolve this?
UPDATE: I just noticed that on my backup file (mednut_bak.sql) there is the proper instruction to create the default ID sequence:
ALTER SEQUENCE public.django_migrations_id_seq OWNED BY public.django_migrations.id;
and
ALTER TABLE ONLY public.django_migrations ALTER COLUMN id SET DEFAULT nextval('public.django_migrations_id_seq'::regclass);
So, I guess problem is while restoring because for some reason Postgres ignores these lines.

(1170, u"BLOB/TEXT column 'user_id' used in key specification without a key length")

I am using a MySQL db in my flask app. I am using SQLAlchemy with pymysql too. The app is deployed on a web server(linux based - specifically Ubuntu). I am trying to apply migrations using flask-migrate but I keep on getting the error
(1170, u"BLOB/TEXT column 'user_id' used in key specification without a key length")
My primary keys look like this:
id = db.Column(db.String(32),default=lambda: str(uuid4().hex), primary_key=True)
The specific table where this error arises is an association table: The code is
user_association_table = db.Table('association', db.Model.metadata,
db.Column('user_id', db.String(32), db.ForeignKey('user.id')),
db.Column('article_id', db.String(32), db.ForeignKey('article.id'))
)
I have seen similar questions but they address how to fix it using MySQL directly. Since I have minimal control of how mysql is generated, how can I bypass this error?
Turns out the original MySQL db that I was trying to apply migrations to was not fully up to date with the size of the primary key --> db.String(32) Deleting and recreating the database made migrations work just fine.
If the database has data then it would be more painful to recreate the database without losing data.

Foreign key issues when copying an InnoDB database

I'm wrestling with an InnoDB MySQL database & not having much luck (normally I'm a MyISAM guy). I'm trying to take the content from a production database and copy it over the development database (e.g. same schema etc.. just hooked up to a different website). Normally in MyISAM I would do this:
1) Dump the prod databse: mysqldump -u root -p prod_db_name > prod_dump.sql
2) Log into mysql terminal;
3) drop database dev_db_name;
4) create database dev_db_name;
5) exit mysql terminal
6) mysql -u root -p dev_db_name < prod_dump.sql
et voila.
However when I do this in this particular situation all seems well until I try to update a content piece using the website hooked to the newly populated dev database; in which case I get this error:
Error Number: 1452
Cannot add or update a child row: a foreign key constraint fails (`abbott/logs`, CONSTRAINT `fk_logs_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`))
Obviously this is related to the users table. Although I'm not sure if it makes a difference, one factor may be that I am not in control of the user id's that populate the users table; they are fed in from an external Single Sign On / Federation API.
I've tried (blind guesses) adding --single-transaction to the mysqldump command; as well as set FOREIGN_KEY_CONSTRAINTS = 0, then manually deleting each table & turning them back on again then reading in the dump (e.g. rather than dropping the whole db).
Any help would be much appreciated.
Reece.

Django switch from sqlite3 to Postgresql issues

I've recently changed the database server on my project from sqlite3 to Postgresql and I have a few questions that I hope will give an answer to my issues.
I understand that switching from sqlite to Postgres implies that I create the new database and the tables inside it, right? I've done that but I haven't seen any new files created in my project to show me that the database I've made is visible. (Btw, I've changed the database name in settings.py)
I probably should mention that I'm working in a virtual environment and I would like to know if that affects my references in any way. I've tried to import the tables in Django to try and count the number of records in a table but I get the error: "No module named psdemo". (psdemo is my database name and i'm trying to import the table with:
from ps.psdemo import Product
where ps is my application, psdemo is my database and Product the table in the database.
In conclusion I'm trying to get access to my database and tables but I can't manage to find them. I repeat, there is no new database file in my project or in my virtual environment (I've searched thoroughly) but if I use a terminal connection I can connect to my virtual environment and change directories to get to the application folder then if I connect to the Postgresql server I can create the database, the tables and can Insert into them, make queries etc, but I cannot access them from the Django code.
I understand that switching from sqlite to Postgres implies that I create the new database and the tables inside it, right? I've done that but I haven't seen any new files created in my project to show me that the database I've made is visible. (Btw, I've changed the database name in settings.py)
All you have to do with postgres is create the database. Not the tables. Django will create the tables, and anything else it thinks are useful, once you call syncdb.
You won't have any new files in your project like you did in sqlite. If you want to view your database, you should download and install pgadminIII (which I would recommend in any event)
I probably should mention that I'm working in a virtual environment and I would like to know if that affects my references in any way. I've tried to import the tables in Django to try and count the number of records in a table but I get the error: "No module named psdemo". (psdemo is my database name and i'm trying to import the table with:
Here, you import models via normal python syntax and it then references your tables. Each model should represent a single table. You define your models first, and then call
python manage.py syncdb
In conclusion I'm trying to get access to my database and tables but I can't manage to find them.
See above, but you should definitely read about postgres installation from the postgres docs, and read the psycopg2 docs as well as the Django docs for setting up a postgres database.
I understand that switching from sqlite to Postgres implies that I
create the new database and the tables inside it, right? I've done
that but I haven't seen any new files created in my project to show me
that the database I've made is visible. (Btw, I've changed the
database name in settings.py)
Database files are not created in the project directory with postgresql. They are created in the database server data directory (like /var/lib/postgres it depends on the distribution). You should generally query it through a PostgreSQL client that connects to the PostgreSQL server rather than messing with the files directly.
You can for example run command:
manage.py dbshell
As to your first issue, see #jpic's answer.
On your second issue, your database is not a package, and you do not import models from your database. If you were able to import your models correctly before you made any changes, change your import statements back to how they were.