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

How to run the FastAPIQuickCRUD is NOT Clear #29

Open
pvssrikanth opened this issue Nov 7, 2022 · 33 comments
Open

How to run the FastAPIQuickCRUD is NOT Clear #29

pvssrikanth opened this issue Nov 7, 2022 · 33 comments
Labels
documentation Improvements or additions to documentation

Comments

@pvssrikanth
Copy link

Dear Sir,
I have installed the FastAPIQuickCRUD and also generator.

BUT How to run and which path IS NOT clear in the README File.

Please guide

Also Where should the user setup the variable SQLALCHEMY

Please reply

Regards,

@LuisLuii
Copy link
Owner

LuisLuii commented Nov 7, 2022

umm, I think just follow the readme is ok. And the FastAPIQuickCRUD and fastapi-crud-project-generator are different project.
you can understand that the fastapi-crud-project-generator is the code gen version of FastAPIQuickCRUD.
free feel to let me know if you have any question, I will reply u ASAP

@LuisLuii
Copy link
Owner

LuisLuii commented Nov 7, 2022

For example in FastAPIQuickCRUD, you can install pip install SQLAlchemy and pip install fastapi-quickcrud, then run it directly by following code (use in-memory db) and

import uvicorn
from fastapi import FastAPI
from sqlalchemy import Column, Integer, \
    String, Table, ForeignKey, orm
from fastapi_quickcrud import crud_router_builder

Base = orm.declarative_base()


class User(Base):
    __tablename__ = 'test_users'
    id = Column(Integer, primary_key=True, autoincrement=True, unique=True)
    name = Column(String, nullable=False)
    email = Column(String, nullable=False)


friend = Table(
    'test_friend', Base.metadata,
    Column('id', ForeignKey('test_users.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False),
    Column('friend_name', String, nullable=False)
)

crud_route_1 = crud_router_builder(db_model=User,
                                   prefix="/user",
                                   tags=["User"],
                                   async_mode=True
                                   )
crud_route_2 = crud_router_builder(db_model=friend,
                                   prefix="/friend",
                                   tags=["friend"],
                                   async_mode=True
                                   )

app = FastAPI()
app.include_router(crud_route_1)
app.include_router(crud_route_2)
uvicorn.run(app, host="0.0.0.0", port=8000, debug=False)

Then try with going to localhost:8000/docs

@LuisLuii LuisLuii added the documentation Improvements or additions to documentation label Nov 7, 2022
@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 7, 2022 via email

@LuisLuii
Copy link
Owner

LuisLuii commented Nov 7, 2022

I see, seem u can run the above example normally, so u are trying to connect to your PostgreSQL DB

  1. I would suggest you use sqlacodegen to generate SQLAlchemy models for your table / or write it by your self.
  2. use the following example code and rewrite your Sqlalchemy model, database dns...
import uvicorn
from fastapi import FastAPI
from fastapi_quickcrud import CrudMethods as CrudRouter
from fastapi_quickcrud import crud_router_builder
from sqlalchemy import *
from sqlalchemy.dialects.postgresql import *
from sqlalchemy.orm import *

app = FastAPI()

Base = declarative_base()
metadata = Base.metadata

from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession

engine = create_async_engine('postgresql+asyncpg://postgres:1234@127.0.0.1:5432/postgres', future=True, echo=True,
                             pool_use_lifo=True, pool_pre_ping=True, pool_recycle=7200)
async_session = sessionmaker(bind=engine, class_=AsyncSession)


async def get_transaction_session() -> AsyncSession:
    async with async_session() as session:
        async with session.begin():
            yield session


class TestUuidPrimarySync(Base):
    __tablename__ = 'test_uuid_primary_sync'
    __table_args__ = (
        PrimaryKeyConstraint('primary_key', name='test_uuid_primary_sync_pkey'),
        UniqueConstraint('primary_key', 'int4_value', 'float4_value',
                         name='test_uuid_primary_sync_primary_key_int4_value_float4_value_key')
    )

    primary_key = Column(UUID(as_uuid=True
                              ))
    bool_value = Column(Boolean, nullable=False, server_default=text('false'))
    float4_value = Column(Float(53), nullable=False)
    float8_value = Column(Float(53), nullable=False, server_default=text('10.10'))
    int2_value = Column(SmallInteger, nullable=False)
    int4_value = Column(Integer, nullable=False)
    char_value = Column(CHAR(10))
    date_value = Column(Date, server_default=text('now()'))
    int8_value = Column(BigInteger, server_default=text('99'))
    interval_value = Column(INTERVAL)
    json_value = Column(JSON)
    jsonb_value = Column(JSONB)
    numeric_value = Column(Numeric)
    text_value = Column(Text)
    time_value = Column(Time)
    timestamp_value = Column(DateTime)
    timestamptz_value = Column(DateTime(True))
    timetz_value = Column(Time(True))
    varchar_value = Column(String)
    array_value = Column(ARRAY(Integer()))
    array_str__value = Column(ARRAY(String()))



router_1 = crud_router_builder(db_session=get_transaction_session,
                               db_model=TestUuidPrimarySync,
                               prefix="/router_1",
                               async_mode=True,
                               crud_methods=CrudRouter.get_declarative_model_full_crud_method(),
                               exclude_columns=[],
                               tags=["test"]
                               )

[app.include_router(i) for i in [router_1]]
uvicorn.run(app, host="0.0.0.0", port=8000, debug=False)

  1. then it should works

for the above example table creation sql statement is following

CREATE TABLE public.test_uuid_primary_sync (
	primary_key uuid NOT NULL,
	bool_value bool NOT NULL DEFAULT false,
	char_value bpchar(10) NULL,
	date_value date NULL DEFAULT now(),
	float4_value float8 NOT NULL,
	float8_value float8 NOT NULL DEFAULT 10.10,
	int2_value int2 NOT NULL,
	int4_value int4 NOT NULL,
	int8_value int8 NULL DEFAULT 99,
	interval_value interval NULL,
	json_value json NULL,
	jsonb_value jsonb NULL,
	numeric_value numeric NULL,
	text_value text NULL,
	time_value time NULL,
	timestamp_value timestamp NULL,
	timestamptz_value timestamptz NULL,
	timetz_value timetz NULL,
	varchar_value varchar NULL,
	array_value _int4 NULL,
	array_str__value _varchar NULL,
	CONSTRAINT test_uuid_primary_sync_pkey PRIMARY KEY (primary_key),
	CONSTRAINT test_uuid_primary_sync_primary_key_int4_value_float4_value_key UNIQUE (primary_key, int4_value, float4_value)
);

@LuisLuii
Copy link
Owner

LuisLuii commented Nov 7, 2022

This project can only provide u some CRUD API with some basic query (try the first example to connect to in-memory in this issue)

On the other head, if you want to add your own business logic into the api, I would like to suggest u try my another open source project, project generator with this example to connect your Postgresql DB.

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 17, 2022 via email

@LuisLuii
Copy link
Owner

LuisLuii commented Nov 17, 2022

Dear Sir,
Thanks for the reply .

I created models using sqlagen

coding: utf-8

from sqlalchemy import Column, DateTime, Index, Integer, Numeric, String,
Table, UniqueConstraint, text
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata

class TESTTABLE(Base):
tablename = 'Testtable'
table_args = (
UniqueConstraint('TXT_INSURER_CODE', 'TXT_TRANSACTION_ID',
'LOAD_MONTH', 'RECONCILE_FLAG'),
)

id = Column(Integer, primary_key=True,

server_default=text("nextval('"HLT_CLAIM_2020_2021_GOVTs_id_seq"'::regclass)"))
....
...

I created these models in db.py file

And then copied your refrence code changed the sqlalchemy to
postgressql suitable and ran python main.py

but getting following error

server:/tmp/fastapi-crud-project-generator$ python main.py
Traceback (most recent call last):
File "main.py", line 3, in
from fastapi_quickcrud import CrudMethods as CrudRouter
File
"/tmp/testenv/lib/python3.8/site-packages/fastapi_quickcrud/init.py",
line 1, in
from .misc.utils import sqlalchemy_to_pydantic
File
"/tmp/testenv/lib/python3.8/site-packages/fastapi_quickcrud/misc/utils.py",
line 14, in
from .crud_model import RequestResponseModel, CRUDModel
File
"/tmp/testenv/lib/python3.8/site-packages/fastapi_quickcrud/misc/crud_model.py",
line 13, in
class RequestResponseModel(BaseModel):
File "pydantic/main.py", line 198, in pydantic.main.ModelMetaclass.new
File "pydantic/fields.py", line 506, in pydantic.fields.ModelField.infer
File "pydantic/fields.py", line 436, in
pydantic.fields.ModelField.init
File "pydantic/fields.py", line 557, in pydantic.fields.ModelField.prepare
File "pydantic/fields.py", line 831, in
pydantic.fields.ModelField.populate_validators
File "pydantic/validators.py", line 765, in find_validators
RuntimeError: no validator found for <class
'pydantic.main.ModelMetaclass'>, see arbitrary_types_allowed in Config

Could you kindly guide What else changes i have to make . THIS IS REALLY
VERY EXCITING PROJECT IF it works for me.

Thanking you
srikanth

On Mon, Nov 7, 2022 at 8:01 PM Luis @.***> wrote:

on the other head, if you want to add some business logic into the api, I
would like to suggest u try my another open source project, project
generator https://github.com/LuisLuii/fastapi-crud-project-generator
with this example
https://github.com/LuisLuii/fastapi-crud-project-generator/blob/main/tutorial/generate_project_with_postgres_db_uuid_pk.py


Reply to this email directly, view it on GitHub
#29 (comment),
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AEGAGU42MYZIMOVTO7J6T5TWHEHCLANCNFSM6AAAAAARZC3RSE
.
You are receiving this because you authored the thread.Message ID:
@.***>

Seems your table has included a field that type did not have type validator. Could u share the Sqlalchemy schema for double check on my side?

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 17, 2022 via email

@LuisLuii
Copy link
Owner

LuisLuii commented Nov 17, 2022

sorry, I mean full code of this thing

class TESTTABLE(Base):
    __tablename__ = 'Testtable'
    __table_args__ = (
    )

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 17, 2022 via email

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 17, 2022 via email

@LuisLuii
Copy link
Owner

LuisLuii commented Nov 17, 2022

Hello, I have tested the following code by your Sqlalchemy schema and it works fine

from fastapi import FastAPI
from sqlalchemy import *
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import *

from fastapi_quickcrud.crud_router import crud_router_builder

Base = declarative_base()
metadata = Base.metadata

engine = create_async_engine('postgresql+asyncpg://postgres:1234@127.0.0.1:5432/postgres', future=True, echo=True,
                             pool_use_lifo=True, pool_pre_ping=True, pool_recycle=7200)
async_session = sessionmaker(bind=engine, class_=AsyncSession)


async def get_transaction_session() -> AsyncSession:
    async with async_session() as session:
        async with session.begin():
            yield session


class xxx(Base):
    __tablename__ = 'xxxx'
    __table_args__ = (
    )
xxxxx



crud_route_1 = crud_router_builder(db_session=get_transaction_session,
                                 db_model=xxxxx,
                                 prefix="/test_1",
                                 tags=["test_1"],
                                 async_mode=True
                                 )


app = FastAPI()
[app.include_router(i) for i in [crud_route_1]]


import uvicorn

uvicorn.run(app, host="0.0.0.0", port=8080)

@LuisLuii
Copy link
Owner

LuisLuii commented Nov 17, 2022

but I cannot make a api test since I don't have data of the table, I think It should works

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 17, 2022 via email

@LuisLuii
Copy link
Owner

welcome to let me know if it's works, and if you have any confidential policy I can hide the Sqlalchemy schema code for the above comment

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 17, 2022 via email

@LuisLuii
Copy link
Owner

LuisLuii commented Nov 17, 2022

could you try to create a new virtual env, and install the package pip install fastapi_quickcrud and run your code. if still not works, welcome to chat with me on telegram @llui1123

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 17, 2022 via email

@LuisLuii
Copy link
Owner

LuisLuii commented Nov 17, 2022

Happy to know that this project can help you well. Btw, welcome to use another project of mine if you need to customize the api router
crud code gen

from sqlalchemy import *
from sqlalchemy.orm import declarative_base

from fastapi_quickcrud_codegen.db_model import DbModel

Base = declarative_base()
metadata = Base.metadata


class xxxxx(Base):
    __tablename__ = 'xxx'
    __table_args__ = (
    )

    xxxxx


from fastapi_quickcrud_codegen import crud_router_builder

model_list = [DbModel(db_model=xxxxx, prefix="/my_first_api", tags=["sample api"])]
crud_router_builder(
    db_model_list=model_list,
    is_async=True,
    database_url="postgresql+asyncpg://postgres:1234@127.0.0.1:5432/postgres"
)

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 17, 2022 via email

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 17, 2022 via email

@LuisLuii
Copy link
Owner

What is this project purpose sir Please explain in brief My requirement is I have legacy database in postgresql and MSSQL These database tables should be EXPOSED with High performance API ( CRUD ) The stakeholders will push multiple records in POST and we should be able to validate using pydantic , throw success records inserted in DB and failed recorrds similarly search. If any of your project meets this requirement WHICH i can extend. I will be very grateful to you. Thanking you in advance. Please also remove some confidential code fragments of my table name etc.

It has the same purpose with this project, but it supports code generation so that you can extend the code easily, such as add some business logic in the api router or modify the Pedantic/Dataclass model, api end point etc...

@LuisLuii
Copy link
Owner

Dear Sir, I tried this project server:/tmp/fastapi-crud-template-generator-main$ python crud.py Traceback (most recent call last): File "crud.py", line 4, in from fastapi_quickcrud_codegen.db_model import DbModel ModuleNotFoundError: No module named 'fastapi_quickcrud_codegen' (myenv)git-server:/tmp/fastapi-crud-template-generator-main$ pip install fastapi_quickcrud_codegen ERROR: Could not find a version that satisfies the requirement fastapi_quickcrud_codegen (from versions: none) ERROR: No matching distribution found for fastapi_quickcrud_codegen (myenv) @.:/tmp/fastapi-crud-template-generator-main$ How do i fix this problem? Thanks On Thu, Nov 17, 2022 at 6:34 PM Srikanth Pokkuluri @.> wrote:

it should be pip install fastapi-crud-code-generator

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 17, 2022 via email

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 18, 2022 via email

@LuisLuii
Copy link
Owner

Dear Sir, I am trying to autogen from MS SQL Server But failing to create classes with some security error I tried all combinations sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed:self signed certificate] (-1) (SQLDriverConnect)') Any hint what i should do? And when i ran crud.py application GetMany Its failing 022-11-18 11:13:16,771 INFO sqlalchemy.engine.Engine [generated in 0.00078s] (Decimal('50000'), Decimal('100000')) 2022-11-18 11:27:31,155 INFO sqlalchemy.engine.Engine COMMITKilled (crudenv) server:/tmp/fastapi-crud-template-generator-main$ Any suggestions. Because the table size is very huge in crore records SO IS It failing becuase of table size ? Please suggest and advice Thanks On Thu, Nov 17, 2022 at 7:39 PM Srikanth Pokkuluri @.***> wrote:

sorry, your questions are a bit confusing and I don't understand it. please asking and showing the code. thanks

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 18, 2022 via email

@LuisLuii
Copy link
Owner

I cannot see the the images from your comment, could you try to share the image by another way?

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 18, 2022 via email

@LuisLuii
Copy link
Owner

LuisLuii commented Nov 18, 2022

still cannot receive you image. Seems you are reply this issue(#29) by email, but GitHub did not support that receive attachment from email.

I have tried you table schema. I create a table by your schema (change server_default to default with dummy data), and insert a dummy data, and request with get many api, all is works normally.
As I know is, you are try to request get many api, but some error is raising up, you could share me the error trackback message

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 29, 2022 via email

@LuisLuii
Copy link
Owner

Hi Sir Again trying to use crud template main I have to show only selected field for GET operation end point instead of showing all fields And search should retrieve in nanoseconds Please advise how to modify and do

crud_methods argument in DbModel is supportting CrudMethods.FIND_ONE, it allow you to generate the find one api only, just like the following example in README

from fastapi_quickcrud_codegen.db_model import DbModel
from fastapi_quickcrud_codegen.misc.type import CrudMethods

from fastapi_quickcrud_codegen import crud_router_builder

model_list = [DbModel(db_model=SampleTableTwo, prefix="/my_second_api", tags=["sample api"],
                      exclude_columns=['bytea_value'],crud_methods=[CrudMethods.FIND_ONE])]
crud_router_builder(
    db_model_list=model_list,
    is_async=False,
    database_url="sqlite://"
)

Then, the retrieval time(nano query) Is not on my design in this project since which involves many situations, such as sql index, hardware, network, sql statement. But you can try to update your own sql statement instead of generated code

@pvssrikanth
Copy link
Author

pvssrikanth commented Nov 30, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants