Update job post insertion method to commit and refresh after adding a new job post
parent
1164a1cb2d
commit
0038157eb0
|
|
@ -0,0 +1,57 @@
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
|
import db_setup
|
||||||
|
from job.job_post.models import JobPostStatusHistory, JobPosts
|
||||||
|
|
||||||
|
print("JobPosts.schema", JobPosts.__table__.schema)
|
||||||
|
print("History.schema", JobPostStatusHistory.__table__.schema)
|
||||||
|
print("metadata.schema", JobPosts.__table__.metadata.schema)
|
||||||
|
for fk in JobPostStatusHistory.__table__.foreign_keys:
|
||||||
|
print("FK", fk.target_fullname, "schema", fk.column.table.schema)
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
async with db_setup.get_engine().begin() as conn:
|
||||||
|
rows = (
|
||||||
|
await conn.execute(
|
||||||
|
text(
|
||||||
|
"SELECT n.nspname AS schema, c.relname AS table "
|
||||||
|
"FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace "
|
||||||
|
"WHERE c.relname IN ('job_posts', 'job_post_status_history') "
|
||||||
|
"ORDER BY 1, 2"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
).all()
|
||||||
|
print("TABLES", rows)
|
||||||
|
fks = (
|
||||||
|
await conn.execute(
|
||||||
|
text(
|
||||||
|
"SELECT con.conname, nsp.nspname AS src_schema, rel.relname AS src_table, "
|
||||||
|
"fnsp.nspname AS dst_schema, frel.relname AS dst_table, "
|
||||||
|
"pg_get_constraintdef(con.oid) AS def "
|
||||||
|
"FROM pg_constraint con "
|
||||||
|
"JOIN pg_class rel ON rel.oid = con.conrelid "
|
||||||
|
"JOIN pg_namespace nsp ON nsp.oid = rel.relnamespace "
|
||||||
|
"JOIN pg_class frel ON frel.oid = con.confrelid "
|
||||||
|
"JOIN pg_namespace fnsp ON fnsp.oid = frel.relnamespace "
|
||||||
|
"WHERE rel.relname = 'job_post_status_history'"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
).all()
|
||||||
|
print("FKS")
|
||||||
|
for r in fks:
|
||||||
|
print(" ", dict(r._mapping))
|
||||||
|
print("search_path", (await conn.execute(text("SHOW search_path"))).scalar())
|
||||||
|
print("app.job_posts", (await conn.execute(text("SELECT count(*) FROM app.job_posts"))).scalar())
|
||||||
|
try:
|
||||||
|
print(
|
||||||
|
"public.job_posts",
|
||||||
|
(await conn.execute(text("SELECT count(*) FROM public.job_posts"))).scalar(),
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print("public.job_posts ERR", type(e).__name__, e)
|
||||||
|
|
||||||
|
|
||||||
|
asyncio.run(main())
|
||||||
|
|
@ -301,7 +301,9 @@ class JobPosts(SQLModel, table=True):
|
||||||
async def insert_job_post(cls, session: AsyncSession, fields: dict):
|
async def insert_job_post(cls, session: AsyncSession, fields: dict):
|
||||||
row = cls(**fields)
|
row = cls(**fields)
|
||||||
session.add(row)
|
session.add(row)
|
||||||
await session.flush()
|
await session.commit()
|
||||||
|
await session.refresh(row)
|
||||||
|
session.flush()
|
||||||
session.add(JobPostStatusHistory(
|
session.add(JobPostStatusHistory(
|
||||||
job_post_id=row.id,
|
job_post_id=row.id,
|
||||||
from_status=None,
|
from_status=None,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue