1+ import os
2+
13from fastapi import FastAPI
2- from fastapi_quickcrud import crud_router_builder
4+ from sqlalchemy .dialects .postgresql import INTERVAL , JSONB , UUID
5+
6+ from fastapi_quickcrud import crud_router_builder , CrudMethods
37from sqlalchemy import *
48from sqlalchemy .orm import *
59from fastapi_quickcrud .crud_router import generic_sql_crud_router_builder
610
711Base = declarative_base ()
812
13+ TEST_DATABASE_URL = os .environ .get ('TEST_DATABASE_URL' , 'postgresql://postgres:1234@127.0.0.1:5432/postgres' )
14+
15+ app = FastAPI ()
16+
17+ Base = declarative_base ()
18+ metadata = Base .metadata
19+
20+ from sqlalchemy import create_engine
21+
22+ engine = create_engine (TEST_DATABASE_URL , future = True , echo = True ,
23+ pool_use_lifo = True , pool_pre_ping = True , pool_recycle = 7200 )
24+ async_session = sessionmaker (autocommit = False , autoflush = False , bind = engine )
25+
26+
27+ def get_transaction_session ():
28+ try :
29+ db = async_session ()
30+ yield db
31+ finally :
32+ db .close ()
33+
34+
35+ class UUIDTable (Base ):
36+ primary_key_of_table = "primary_key"
37+ unique_fields = ['primary_key' , 'int4_value' , 'float4_value' ]
38+ __tablename__ = 'test_uuid_primary_sync'
39+ __table_args__ = (
40+ UniqueConstraint ('primary_key' , 'int4_value' , 'float4_value' ),
41+ )
42+ primary_key = Column (Integer , primary_key = True , info = {'alias_name' : 'primary_key' }, autoincrement = True ,
43+ server_default = "nextval('test_build_myself_id_seq'::regclass)" )
44+ bool_value = Column (Boolean , nullable = False , server_default = text ("false" ))
45+ char_value = Column (CHAR (10 ))
46+ date_value = Column (Date , server_default = text ("now()" ))
47+ float4_value = Column (Float , nullable = False )
48+ float8_value = Column (Float (53 ), nullable = False , server_default = text ("10.10" ))
49+ int2_value = Column (SmallInteger , nullable = False )
50+ int4_value = Column (Integer , nullable = False )
51+ int8_value = Column (BigInteger , server_default = text ("99" ))
52+ interval_value = Column (INTERVAL )
53+ json_value = Column (JSON )
54+ jsonb_value = Column (JSONB (astext_type = Text ()))
55+ numeric_value = Column (Numeric )
56+ text_value = Column (Text )
57+ time_value = Column (Time )
58+ timestamp_value = Column (DateTime )
59+ timestamptz_value = Column (DateTime (True ))
60+ timetz_value = Column (Time (True ))
61+ varchar_value = Column (String )
62+ array_value = Column (ARRAY (Integer ()))
63+ array_str__value = Column (ARRAY (String ()))
64+
65+
66+ UUIDTable .__table__ .create (engine , checkfirst = True )
67+
968
1069class Account (Base ):
1170 __tablename__ = "account"
@@ -29,33 +88,16 @@ class BlogComment(Base):
2988
3089
3190crud_route_parent = crud_router_builder (
32- db_model = Account ,
91+ db_model = UUIDTable ,
3392 prefix = "/account" ,
3493 tags = ["account" ],
35- foreign_include = [BlogComment , BlogPost ]
36-
37- )
38- print ("BlogPost222" )
39-
40- crud_route_child1 = generic_sql_crud_router_builder (
41- db_model = BlogPost ,
42- prefix = "/blog_post" ,
43- tags = ["blog_post" ],
44- foreign_include = [BlogComment , Account ]
45-
46- )
47- print ("BlogComment111" )
48-
49- crud_route_child2 = generic_sql_crud_router_builder (
50- db_model = BlogComment ,
51- prefix = "/blog_comment" ,
52- tags = ["blog_comment" ],
53- foreign_include = [BlogPost ]
94+ crud_methods = [CrudMethods .UPDATE_MANY ],
95+ db_session = get_transaction_session ,
5496
5597)
5698
5799app = FastAPI ()
58- [app .include_router (i ) for i in [crud_route_parent , crud_route_child1 , crud_route_child2 ]]
100+ [app .include_router (i ) for i in [crud_route_parent ]]
59101
60102
61103@app .get ("/" , tags = ["child" ])
@@ -65,4 +107,5 @@ async def root():
65107
66108import uvicorn
67109
68- uvicorn .run (app , host = "0.0.0.0" , port = 8002 , debug = False )
110+ uvicorn .run (app , host = "0.0.0.0" , port = 8003
111+ , debug = False )
0 commit comments