Issue: peewee.InterfaceError: Error, database must be initialized before opening a connection.
Author: NPZYMR
State: closed
Created: 2023-02-25 22:25:31

import os
from playhouse.postgres_ext import CharField, DateTimeField, IntegerField, BooleanField
from .base import Base
import datetime
from peewee import PostgresqlDatabase
from playhouse.migrate import *

db_name = os.environ.get('DB_NAME')
db_host = os.environ.get('DB_HOST')
db_port = os.environ.get('DB_PORT')
db_password = os.environ.get('DB_PASSWORD')
db_user= os.environ.get('DB_USER')
# Initialize database.

pg_db = PostgresqlExtDatabase(db_name, user=db_user, password=db_password,host=db_host, port=db_port)
migrator = PostgresqlMigrator(pg_db)

class EmpDetails(Base):
    id = CharField(25, null=False)
    email = CharField(255, unique=True)
    name = CharField(255)
    location = CharField(255)
    created_at = DateTimeField(default=datetime.datetime.now)
    updated_at = DateTimeField(default=datetime.datetime.now)
    is_active = BooleanField(default=True)
    role = CharField(25)

    class Meta:
        table_name = 'emp_details'

test_field = CharField(default='') 

migrate(
    
    migrator.add_column('emp_details', 'test', test_field),
)

I'm trying to make my DB instance for migration but getting an error :
```
File "/home/naveen.p@ah.zymrinc.com/Desktop/zfr/server/s-venv/lib/python3.8/site-packages/peewee.py", line 3026, in connect
    raise InterfaceError('Error, database must be initialized '
peewee.InterfaceError: Error, database must be initialized before opening a connection.
```

If you had taken a moment to debug this, you would have found that `db_name = os.environ.get('DB_NAME')` is somehow getting set to `None`. This is the issue.

In debug, I'm getting my DB name in "db_name = os.environ.get('DB_NAME')" but
don't know how to rectify this now. please don't mind this I'm new to
programming might sound silly.

On Sun, Feb 26, 2023 at 4:13 AM Charles Leifer ***@***.***>
wrote:

> If you had taken a moment to debug this, you would have found that db_name
> = os.environ.get('DB_NAME') is somehow getting set to None. This is the
> issue.
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/coleifer/peewee/issues/2682#issuecomment-1445220548>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AYITTXNG2NCYUIK3EDA2Y3TWZKDHNANCNFSM6AAAAAAVIC6EQA>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>

-- 
============================================================================
IMPORTANT NOTICE: The information in this e-mail and any attached files is 
CONFIDENTIAL and may be legally privileged or prohibited from disclosure 
and unauthorised use. The views of the author may not necessarily reflect 
those of Zymr, Inc. It is intended solely for the addressee, or the 
employee or agent responsible for delivering such materials to the 
addressee. If you have received this message in error please return it to 
the sender then delete the email and destroy any copies of it. If you are 
not the intended recipient, any form of reproduction, dissemination, 
copying, disclosure, modification, distribution and/or publication, or any 
action taken or omitted to be taken in reliance upon this message or its 
attachments is prohibited and may be unlawful. At present the integrity of 
e-mail across the Internet cannot be guaranteed and messages sent via this 
medium are potentially at risk. All liability is excluded to the extent 
permitted by law for any claims arising as a result of the use of this 
medium to transmit information by or to Zymr, Inc.


You would want to double-check what you're specifying wherever you have your `PostgresqlDatabase(...)` constructor, e.g.:

```
assert db_name, 'db_name is empty!'
pg_db = PostgresqlExtDatabase(db_name, user=db_user, password=db_password,host=db_host, port=db_port)
```

Thank you for your help, I'll check it out.

On Sun, Feb 26, 2023 at 4:27 AM Charles Leifer ***@***.***>
wrote:

> You would want to double-check what you're specifying wherever you have
> your PostgresqlDatabase(...) constructor, e.g.:
>
> assert db_name, 'db_name is empty!'
> pg_db = PostgresqlExtDatabase(db_name, user=db_user, password=db_password,host=db_host, port=db_port)
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/coleifer/peewee/issues/2682#issuecomment-1445222329>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AYITTXMRJVQ5CQBNMEH2RDDWZKE3XANCNFSM6AAAAAAVIC6EQA>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>

-- 
============================================================================
IMPORTANT NOTICE: The information in this e-mail and any attached files is 
CONFIDENTIAL and may be legally privileged or prohibited from disclosure 
and unauthorised use. The views of the author may not necessarily reflect 
those of Zymr, Inc. It is intended solely for the addressee, or the 
employee or agent responsible for delivering such materials to the 
addressee. If you have received this message in error please return it to 
the sender then delete the email and destroy any copies of it. If you are 
not the intended recipient, any form of reproduction, dissemination, 
copying, disclosure, modification, distribution and/or publication, or any 
action taken or omitted to be taken in reliance upon this message or its 
attachments is prohibited and may be unlawful. At present the integrity of 
e-mail across the Internet cannot be guaranteed and messages sent via this 
medium are potentially at risk. All liability is excluded to the extent 
permitted by law for any claims arising as a result of the use of this 
medium to transmit information by or to Zymr, Inc.


After migration do I need to remove migrate(.... ) function from my model
file?

How will I add new model field in my existing model class(python code)?

-- 
============================================================================
IMPORTANT NOTICE: The information in this e-mail and any attached files is 
CONFIDENTIAL and may be legally privileged or prohibited from disclosure 
and unauthorised use. The views of the author may not necessarily reflect 
those of Zymr, Inc. It is intended solely for the addressee, or the 
employee or agent responsible for delivering such materials to the 
addressee. If you have received this message in error please return it to 
the sender then delete the email and destroy any copies of it. If you are 
not the intended recipient, any form of reproduction, dissemination, 
copying, disclosure, modification, distribution and/or publication, or any 
action taken or omitted to be taken in reliance upon this message or its 
attachments is prohibited and may be unlawful. At present the integrity of 
e-mail across the Internet cannot be guaranteed and messages sent via this 
medium are potentially at risk. All liability is excluded to the extent 
permitted by law for any claims arising as a result of the use of this 
medium to transmit information by or to Zymr, Inc.


```class User(Model):
    pass

f = TextField()
User._meta.add_field('username', f)
print(User._meta.fields)# {'id': <peewee.AutoField at
0x7f552815bc50>,#  'username': <peewee.TextField at 0x7f552b1d7e10>}
```


I've read one reply of yours in which you are referring to this. Is
that referring to what I want.


On Mon, Feb 27, 2023 at 1:07 AM Naveen Pandia ***@***.***>
wrote:

> After migration do I need to remove migrate(.... ) function from my model
> file?
>
> How will I add new model field in my existing model class(python code)?
>

-- 
============================================================================
IMPORTANT NOTICE: The information in this e-mail and any attached files is 
CONFIDENTIAL and may be legally privileged or prohibited from disclosure 
and unauthorised use. The views of the author may not necessarily reflect 
those of Zymr, Inc. It is intended solely for the addressee, or the 
employee or agent responsible for delivering such materials to the 
addressee. If you have received this message in error please return it to 
the sender then delete the email and destroy any copies of it. If you are 
not the intended recipient, any form of reproduction, dissemination, 
copying, disclosure, modification, distribution and/or publication, or any 
action taken or omitted to be taken in reliance upon this message or its 
attachments is prohibited and may be unlawful. At present the integrity of 
e-mail across the Internet cannot be guaranteed and messages sent via this 
medium are potentially at risk. All liability is excluded to the extent 
permitted by law for any claims arising as a result of the use of this 
medium to transmit information by or to Zymr, Inc.