# Path: psqlextra/query.py
# class PostgresQuerySet(models.QuerySet):
#     def __init__(self, model=None, query=None, using=None, hints=None):
#     def annotate(self, **annotations):
#     def rename_annotations(self, **annotations):
#     def on_conflict(
#         self,
#         fields: ConflictTarget,
#         action: ConflictAction,
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ):
#     def bulk_insert(
#         self,
#         rows: List[dict],
#         return_model: bool = False,
#         using: Optional[str] = None,
#     ):
#     def insert(self, using: Optional[str] = None, **fields):
#     def insert_and_get(self, using: Optional[str] = None, **fields):
#     def upsert(
#         self,
#         conflict_target: ConflictTarget,
#         fields: dict,
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         using: Optional[str] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ) -> int:
#     def upsert_and_get(
#         self,
#         conflict_target: ConflictTarget,
#         fields: dict,
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         using: Optional[str] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ):
#     def bulk_upsert(
#         self,
#         conflict_target: ConflictTarget,
#         rows: Iterable[Dict],
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         return_model: bool = False,
#         using: Optional[str] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ):
#         def is_empty(r):
#     def _create_model_instance(
#         self, field_values: dict, using: str, apply_converters: bool = True
#     ):
#     def _build_insert_compiler(
#         self, rows: Iterable[Dict], using: Optional[str] = None
#     ):
#     def _is_magical_field(self, model_instance, field, is_insert: bool):
#     def _get_upsert_fields(self, kwargs):
# 
# Path: tests/fake_model.py
# def get_fake_model(fields=None, model_base=PostgresModel, meta_options={}):
#     """Defines a fake model and creates it in the database."""
# 
#     model = define_fake_model(fields, model_base, meta_options)
# 
#     with connection.schema_editor() as schema_editor:
#         schema_editor.create_model(model)
# 
#     return model


from django.db import models
from psqlextra.query import ConflictAction
from .fake_model import get_fake_model

    results = model.objects.on_conflict(
        ["name"], ConflictAction.NOTHING
    ).insert_and_get(name="the-object", cookies=cookie_string)

    assert results is not None
    assert results.cookies == cookie_string

    obj1 = model.objects.get()
    assert obj1.cookies == cookie_string


def test_insert_many_to_many():
    """Tests whether adding a rows to a m2m works after using insert_and_get.

    The model returned by `insert_and_get` must be configured in a
    special way. Just creating a instance of the model is not enough to
    be able to add m2m rows.
    """

    model1 = get_fake_model({"name": models.TextField(primary_key=True)})

    model2 = get_fake_model(
        {
            "name": models.TextField(primary_key=True),
            "model1s": models.ManyToManyField(model1),
        }
    )

    row2 = model2.objects.on_conflict(

# Path: psqlextra/query.py
# class PostgresQuerySet(models.QuerySet):
#     def __init__(self, model=None, query=None, using=None, hints=None):
#     def annotate(self, **annotations):
#     def rename_annotations(self, **annotations):
#     def on_conflict(
#         self,
#         fields: ConflictTarget,
#         action: ConflictAction,
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ):
#     def bulk_insert(
#         self,
#         rows: List[dict],
#         return_model: bool = False,
#         using: Optional[str] = None,
#     ):
#     def insert(self, using: Optional[str] = None, **fields):
#     def insert_and_get(self, using: Optional[str] = None, **fields):
#     def upsert(
#         self,
#         conflict_target: ConflictTarget,
#         fields: dict,
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         using: Optional[str] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ) -> int:
#     def upsert_and_get(
#         self,
#         conflict_target: ConflictTarget,
#         fields: dict,
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         using: Optional[str] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ):
#     def bulk_upsert(
#         self,
#         conflict_target: ConflictTarget,
#         rows: Iterable[Dict],
#         index_predicate: Optional[Union[Expression, Q, str]] = None,
#         return_model: bool = False,
#         using: Optional[str] = None,
#         update_condition: Optional[Union[Expression, Q, str]] = None,
#     ):
#         def is_empty(r):
#     def _create_model_instance(
#         self, field_values: dict, using: str, apply_converters: bool = True
#     ):
#     def _build_insert_compiler(
#         self, rows: Iterable[Dict], using: Optional[str] = None
#     ):
#     def _is_magical_field(self, model_instance, field, is_insert: bool):
#     def _get_upsert_fields(self, kwargs):
# 
# Path: tests/fake_model.py
# def get_fake_model(fields=None, model_base=PostgresModel, meta_options={}):
#     """Defines a fake model and creates it in the database."""
# 
#     model = define_fake_model(fields, model_base, meta_options)
# 
#     with connection.schema_editor() as schema_editor:
#         schema_editor.create_model(model)
# 
#     return model


# Path: tests/test_insert.py
from django.db import models
from psqlextra.query import ConflictAction
from .fake_model import get_fake_model


    results = model.objects.on_conflict(
        ["name"], ConflictAction.NOTHING
    ).insert_and_get(name="the-object", cookies=cookie_string)

    assert results is not None
    assert results.cookies == cookie_string

    obj1 = model.objects.get()
    assert obj1.cookies == cookie_string


def test_insert_many_to_many():
    """Tests whether adding a rows to a m2m works after using insert_and_get.

    The model returned by `insert_and_get` must be configured in a
    special way. Just creating a instance of the model is not enough to
    be able to add m2m rows.
    """

    model1 = get_fake_model({"name": models.TextField(primary_key=True)})

    model2 = get_fake_model(
        {
            "name": models.TextField(primary_key=True),
            "model1s": models.ManyToManyField(model1),
        }
    )

    row2 = model2.objects.on_conflict(

        ["name"], ConflictAction.UPDATE