Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Issues connecting to PostgreSQL with Peewee #733

Answered by CodePunk-
CodePunk- asked this question in Q&A
Discussion options

Greetings,

Has anyone had success connecting to PostgreSQL with Peewee? I've created a simple service that should connect to my PostgreSQL database (TimescaleDB add-on) but doesn't matter what I try I keep getting the following error:

[custom_components.pyscript.file.sensor_data.log_sensor_data] Error initializing database: database attribute does not appear to be set on the model: <Model: DeviceLog>

I have added the database attribute to the Meta class of my model and tested my code outside HASS so I know it works. As anyone ran into this issue?

You must be logged in to vote

For those running into the same issue, utilize the @pyscript_executor and move the class models inside the function. Whether this is the right solutions, I do not know. I'm not a python nor a Pyscript expert but it works for my needs. Hope it helps others and thanks to the developers of Pyscript. I find this custom integration easier than some other solutions.

from peewee import Model, CharField, PostgresqlDatabase
# Database configuration
DATABASE_CONFIG = {
 'host': 'timescaledb',
 'database': 'database_name',
 'user': 'user01',
 'password': 'password01',
 'port': 5432
}
# Create database instance
database = PostgresqlDatabase(**DATABASE_CONFIG)
@pyscript_executor
def c...

Replies: 2 comments 1 reply

Comment options

I'm sure it's a complex module; is it async? You will probably need to use it in a native python module perhaps also in its own thread.

You must be logged in to vote
1 reply
Comment options

It's probably not async, but I tried peewee-async (which is async), and I'm still getting the same error.

If I create a native python module, where should I place those files? pyscript/app ?

Comment options

For those running into the same issue, utilize the @pyscript_executor and move the class models inside the function. Whether this is the right solutions, I do not know. I'm not a python nor a Pyscript expert but it works for my needs. Hope it helps others and thanks to the developers of Pyscript. I find this custom integration easier than some other solutions.

from peewee import Model, CharField, PostgresqlDatabase
# Database configuration
DATABASE_CONFIG = {
 'host': 'timescaledb',
 'database': 'database_name',
 'user': 'user01',
 'password': 'password01',
 'port': 5432
}
# Create database instance
database = PostgresqlDatabase(**DATABASE_CONFIG)
@pyscript_executor
def connecting_to_postgresql():
 
 class User(Model):
 class Meta:
 database = database
 
 user = CharField(primary_key=True)
 email= CharField()
 database.connect()
 with database:
 database.create_tables( [User] , safe=True)
 User.create(user="user01", email="user01@example.com")
connecting_to_postgresql()
You must be logged in to vote
0 replies
Answer selected by CodePunk-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /