Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgreSQL: SQLModel.metadata.create_all(engine) doesn't create the database file #66

Open
8 tasks done
Matthieu-Tinycoaching opened this issue Sep 1, 2021 · 1 comment
Labels
question Further information is requested

Comments

@Matthieu-Tinycoaching
Copy link

Matthieu-Tinycoaching commented Sep 1, 2021

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from datetime import datetime
from typing import Optional, Dict
from sqlmodel import Field, SQLModel, create_engine

class SemanticSearch(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    id_user: int
    date_time: datetime
    query: str
    clean_query: str
    
engine = create_engine('postgresql://postgres:postgres@localhost:5432/embeddings_sts_tf', echo=True)

SQLModel.metadata.create_all(engine)

Description

Following the tutorial user guide based on sqlite I tried to do the same with postgresql database, but contrary to sqlite the SQLModel.metadata.create_all(engine) command doesn't seem to create my embeddings_sts_tf postgresql database

Operating System

Linux

Operating System Details

Ubuntu 18.04 LTS

SQLModel Version

0.0.4

Python Version

3.8.8

Additional Context

No response

@Matthieu-Tinycoaching Matthieu-Tinycoaching added the question Further information is requested label Sep 1, 2021
@rodg
Copy link

rodg commented Sep 2, 2021

From this stack-overflow post you can't create a postgresql database via create_engine or create_all. Following the steps in that SO answer works, with the caveat of the SQLAlchemy 2.0 engine not allowing you to just execute on text (and create_engine from SQLModel has future=True by default). For 2.0 you need to use the text() construct to execute text like that. YOLO importing text from SQLModel seems to import the right thing, so you don't even need to import it from SQLAlchemy apparently but I've not seen this import documented anywhere.

from datetime import datetime
from typing import Optional, Dict
from sqlmodel import Field, SQLModel, create_engine, text

class SemanticSearch(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    id_user: int
    date_time: datetime
    query: str
    clean_query: str
    
engine = create_engine('postgresql://postgres:postgres@localhost:5432/postgres', echo=True)

conn = engine.connect()
conn.execute(text("commit"))
conn.execute(text("create database your_db_name"))
conn.close()
conn.dispose()

engine = create_engine('postgresql://postgres:postgres@localhost:5432/your_db_name', echo=True)

SQLModel.metadata.create_all(engine)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants