-
Notifications
You must be signed in to change notification settings - Fork 58
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
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
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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 ?
Beta Was this translation helpful? Give feedback.
All reactions
-
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()
Beta Was this translation helpful? Give feedback.