-
Notifications
You must be signed in to change notification settings - Fork 81
Fix: Table 'langchain_pg_collection' is already defined for this MetaData instance #209
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
Open
0xrushi
wants to merge
5
commits into
langchain-ai:main
Choose a base branch
from
0xrushi:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
import logging | ||
import uuid | ||
import warnings | ||
import threading | ||
from typing import ( | ||
Any, | ||
AsyncGenerator, | ||
|
@@ -100,11 +101,14 @@ class DistanceStrategy(str, enum.Enum): | |
.union(SPECIAL_CASED_OPERATORS) | ||
) | ||
|
||
_embedding_collection_store_lock = threading.Lock() | ||
|
||
def _get_embedding_collection_store(vector_dimension: Optional[int] = None) -> Any: | ||
def _get_embedding_collection_store(vector_dimension: Optional[int] = None, extend_existing: bool = False) -> Any: | ||
global _classes | ||
if _classes is not None: | ||
return _classes | ||
|
||
with _embedding_collection_store_lock: | ||
if _classes is not None: | ||
return _classes | ||
|
||
from pgvector.sqlalchemy import Vector # type: ignore | ||
|
||
|
@@ -113,6 +117,9 @@ class CollectionStore(Base): | |
|
||
__tablename__ = "langchain_pg_collection" | ||
|
||
if extend_existing: | ||
__table_args__ = {'extend_existing': True} | ||
|
||
uuid = sqlalchemy.Column( | ||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4 | ||
) | ||
|
@@ -215,14 +222,25 @@ class EmbeddingStore(Base): | |
document = sqlalchemy.Column(sqlalchemy.String, nullable=True) | ||
cmetadata = sqlalchemy.Column(JSONB, nullable=True) | ||
|
||
__table_args__ = ( | ||
sqlalchemy.Index( | ||
"ix_cmetadata_gin", | ||
"cmetadata", | ||
postgresql_using="gin", | ||
postgresql_ops={"cmetadata": "jsonb_path_ops"}, | ||
), | ||
) | ||
if extend_existing: | ||
__table_args__ = ( | ||
sqlalchemy.Index( | ||
"ix_cmetadata_gin", | ||
"cmetadata", | ||
postgresql_using="gin", | ||
postgresql_ops={"cmetadata": "jsonb_path_ops"}, | ||
), | ||
{'extend_existing': True} | ||
) | ||
else: | ||
__table_args__ = ( | ||
sqlalchemy.Index( | ||
"ix_cmetadata_gin", | ||
"cmetadata", | ||
postgresql_using="gin", | ||
postgresql_ops={"cmetadata": "jsonb_path_ops"}, | ||
), | ||
) | ||
|
||
_classes = (EmbeddingStore, CollectionStore) | ||
|
||
|
@@ -387,6 +405,7 @@ def __init__( | |
use_jsonb: bool = True, | ||
create_extension: bool = True, | ||
async_mode: bool = False, | ||
extend_existing: bool = False, | ||
) -> None: | ||
"""Initialize the PGVector store. | ||
For an async version, use `PGVector.acreate()` instead. | ||
|
@@ -415,6 +434,9 @@ def __init__( | |
create_extension: If True, will create the vector extension if it | ||
doesn't exist. disabling creation is useful when using ReadOnly | ||
Databases. | ||
extend_existing: If True, will set extend_existing=True in table_args for | ||
SQLAlchemy models. This helps prevent race conditions when multiple | ||
threads try to create tables simultaneously. (default: False) | ||
""" | ||
self.async_mode = async_mode | ||
self.embedding_function = embeddings | ||
|
@@ -428,6 +450,7 @@ def __init__( | |
self._engine: Optional[Engine] = None | ||
self._async_engine: Optional[AsyncEngine] = None | ||
self._async_init = False | ||
self.extend_existing = extend_existing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be a private variable self._extend_existing |
||
|
||
if isinstance(connection, str): | ||
if async_mode: | ||
|
@@ -470,7 +493,8 @@ def __post_init__( | |
self.create_vector_extension() | ||
|
||
EmbeddingStore, CollectionStore = _get_embedding_collection_store( | ||
self._embedding_length | ||
self._embedding_length, | ||
extend_existing=self.extend_existing, | ||
) | ||
self.CollectionStore = CollectionStore | ||
self.EmbeddingStore = EmbeddingStore | ||
|
@@ -486,7 +510,8 @@ async def __apost_init__( | |
self._async_init = True | ||
|
||
EmbeddingStore, CollectionStore = _get_embedding_collection_store( | ||
self._embedding_length | ||
self._embedding_length, | ||
extend_existing=self.extend_existing, | ||
) | ||
self.CollectionStore = CollectionStore | ||
self.EmbeddingStore = EmbeddingStore | ||
|
@@ -514,10 +539,13 @@ async def acreate_vector_extension(self) -> None: | |
async with self._async_engine.begin() as conn: | ||
await conn.run_sync(_create_vector_extension) | ||
|
||
_create_tables_lock = threading.Lock() | ||
def create_tables_if_not_exists(self) -> None: | ||
with self._make_sync_session() as session: | ||
Base.metadata.create_all(session.get_bind()) | ||
session.commit() | ||
"""Create tables if they don't exist in a thread-safe manner.""" | ||
with _create_tables_lock: | ||
with self._make_sync_session() as session: | ||
Base.metadata.create_all(session.get_bind()) | ||
session.commit() | ||
|
||
async def acreate_tables_if_not_exists(self) -> None: | ||
assert self._async_engine, "This method must be called with async_mode" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.