some results to the 'No relations found' in PostgreSQL from yesterday, might be a tiny bug

1,027 views
Skip to first unread message

Philip Hu

unread,
Aug 31, 2012, 6:17:27 AM8/31/12
to mezzani...@googlegroups.com
Problem: PostgreSQL keeping says 'No relations found.'

I found out the problem when I tried to work on an inherited class that expands the Product Model in cartridge. 

I have install/re-installed mezzanine/django in a trial-error way to sort out the problem. 

Here are the final results and steps:
1) create new virtualenv, say called 'myenv'.
2) pip install django
3) pip install psycopg2
4) run 'django-admin.py startproject myproj'
5) check the database, and yes, all tables are installed and visible under #\dt+ command.

now within the same virtualenv, I continue the test by:

6) pip install -U cartridge, and create a new database.
7) start a new project: 'mezzanine-project -a cartridge mezzcart'
8) run 'python manage.py createdb --noinput'
9) tables are installed, demo-site at http://127.0.0.1:8000 works! but non of table are visible
Since there is no other schema other than 'public', alter the 'search_path' in PostgreSQL seems do little use. 
I also tried create a schema same to the user's name, and hope any non-decorated 'create table' command will have their tables created under the user's name schema, but no luck, nothing could be seen after mezzanine installation with the search_path updated.  

=> \dt+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+---------+-------+----------+---------+-------------
public | xp_test | table | xiaopeng | 0 bytes |
(1 row)

=> \dn+
List of schemas
Name | Owner | Access privileges | Description
--------+----------+----------------------+------------------------
public | postgres | postgres=UC/postgres+| standard public schema
| | =UC/postgres |
(1 row)

To summarize, i started from a new virtualevn, and the django 1.4.1 is working fine. However, the very same virutalenv gives the above error with mezzanine/cartridge. Since I am not a experienced django/mezzanine/postgreSQL user, I've spend 20+ hours on this very issue in last two days. I wonder anyone experience similar issues? Are there any basic setup I am missing?
This very problem, worries me in the way that I would running to problems later in developments or in production. 

(setup: ubuntu 12.04 + PostgreSQL 9.1 + Virutalenvs. All on a single computer, use localhost, installation mainly reference to ROSS's guide at http://rosslaird.com/blog/first-steps-with-mezzanine/ , )


Philip Hu

unread,
Aug 31, 2012, 6:26:46 PM8/31/12
to mezzani...@googlegroups.com
Now I am pretty sure is mezzanine's 'python manage.py createdb' that lead to the problem.

Stephen McDonald

unread,
Aug 31, 2012, 10:58:55 PM8/31/12
to mezzani...@googlegroups.com
I can't quite gauge whether you're having problems with some custom code you've written (which you first briefly mention), or with getting an initial project set up with Postgres to start with.

As for any custom models you've written, if you're having trouble getting your database synchronised with these, I'd suggest going over the Django docs for how syncdb works, and then once you're confortable with that, going over the south docs, which is a separate library used for managing more advanced database synchronisation and migrations for your models. Mezzanine makes heavy use of south.


As for getting an initial project set up with Postgres, I'll just walk through the steps I take that work without any issues. Firstly, and this may be the key issue, you don't need to run both "django-admin startproject" and "mezzanine-project" - these achieve the same thing, with the latter being Mezzanine's enhanced version that  does everything extra in setting up a Mezzanine project that Django's startproject command doesn't do. So the first of these isn't required.

So in a fresh virtualenv:

Install cartridge and psycopg2 - Mezzanine and Django will be installed as dependencies automatically. We also install south because we've read all the documentation and know we're better off using it:
$ pip install cartridge psycopg2 south

Create a Mezzanine project called myproject, with Cartridge installed:
$ mezzanine-project -a cartridge myproject

Create a database user called myuser with the password mypassword:
$ psql -U postgres -h 127.0.0.1 -c "CREATE USER myusername WITH ENCRYPTED PASSWORD 'mypassword';"

Create a database called mydatabase owned by the new user called myuser:
$ psql -U postgres -h 127.0.0.1 -c "CREATE DATABASE mydatabase WITH OWNER myuser ENCODING = 'UTF8' LC_CTYPE = 'en_US.UTF-8' LC_COLLATE = 'en_US.UTF-8' TEMPLATE template0;"

Then edit myproject/local_settings.py and add the new database settings:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": "mydatabase",
        "USER": "myuser",
        "PASSWORD": "mypassword",
        "HOST": "127.0.0.1",
        "PORT": "",
    }
}

Now with the database created and configured, we can finally set up the database tables with:
$ python myproject/manage.py createdb --noinput

Hope this helps!

--
Stephen McDonald
http://jupo.org

Xiaopeng Hu

unread,
Sep 1, 2012, 4:08:20 AM9/1/12
to mezzani...@googlegroups.com

Hi Stephen, 
thank you very much for to take time to reply with a such detailed instructions and references. They are so helpful to a beginner like me. 

I literally followed your practice on a just formatted/re-installed ubuntu 12.04. Before adapt your instructions, the only thing I did was to install the needed prerequisites: 

  • 1) sudo apt-get install apache2 postgresql postgresql-server-dev-9.1 python-dev python-pip python-setuptools python-imaging build-essential
  • 2) install virtualenv
After followed your instructions, the developer site at http://127.0.0.1:8000 works flawlessly. Thank you.

I guess, at least for now, I have to learn more about how south works and get used to not to manipulate with the data-tables directly, since i still cannot visualize or locate any tables created by Mezzanine/Cartridge project within the postgresql database. 

regards,
philip

GW

unread,
Sep 1, 2012, 6:37:10 PM9/1/12
to mezzani...@googlegroups.com

Hi,

As I already mentioned for locating anything in Postgresql, you need to check that:

- you are connecting to the right host/IP (in Stephen's example: 127.0.0.1) (this is not needed if you are connecting locally and using UNIX sockets)
- you are connecting to the right database (in Stephen's example: mydatabase)
- and using the right schema (probably public or the same as username if not given) (to explicitly enter it in settings.py under DATABASES add: 'SCHEMA': 'myschema',)
- you are connecting with the correct user and password (in Stephen's example he is using the admin user 'postgres', and created a user 'myuser' with 'mypassword' that will be used by Django)

This means that for connecting as admin you use: psql -U postgres -h 127.0.0.1 mydatabase

To list all databases use: \l

To connect to another database use, eg: \c myotherdatabase

For listing schemas in the database where you are connected at the moment: \dn+

To list all tables in your current schema search path: \dt+

To force listing tables from schemas that are not in your search path: \dt+ myschema.*

To see your current schema search path: SHOW search_path;

To change your schema search path: SET search_path TO myschema, public;

All this commands are well described in the official PostgreSQL documentation http://www.postgresql.org/docs/9.1/static/ (also the design philosophy of the database which is good to know if you want to work with it directly) and probably also all PostgreSQL tutorials contain them. There is nothing Django/South/Mezzanine/Cartridge-specific about them.

But anyway, as you have noticed, in Django, South, Mezzanine or Cartridge it is really not recommended to manipulate with tables in the database directly, but instead manipulate with Django models, synchronize them or create database migrations using South. Everything you need can be done through them, even exporting and importing data, so there is really no need to directly manipulate the database (except initially creating a database and a user with permissions on it). Stephen suggested a few good starting points for learning about them. When you get to know them you will never want to use the old-school approaches anymore.

Greetings,
  gw

Reply all
Reply to author
Forward
0 new messages