Flowing large hash values to Postgres BigInt #1499
-
First Check
Commit to Help
Example Codefrom sqlalchemy import BigInteger
from typing import Optional
from sqlmodel import Field, Session, SQLModel, create_engine
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
#case A
normhash: Optional[int] = Field(default=None, index=True)
#case B
#normhash: Optional[BigInteger] = Field(default=None, index=True)
hero_1 = Hero(normhash=1559512409891417611)
engine = create_engine("postgresql://app:newbpw@somehost:5400/some_db)
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
session.add(hero_1)
session.commit()
# in case A: DataError: (psycopg2.errors.NumericValueOutOfRange) integer out of range
# (case B the code won't even finish coming up - no validator error)
session.refresh(hero_1)
print(hero_1) DescriptionUsing your default Hero example. So, case B: trying to force a postgres BIGINT, using sqlalchemy BigInteger, I get: I know it involves all the different levels, but it seems like a model of use problem (and I had validator problems before that ended up being a change in the way I use sqlmodel.) Thanks for your creation of sqlmodel - so far I've really enjoyed it along with fastapi! Operating SystemLinux Operating System DetailsNo response SQLModel Version0.0.4 Python Version3.8.12 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments
-
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
normhash: Optional[int] = Field(default=None, index=True, sa_column=Column(BigInteger())) UPD YuriiMotov: to avoid conflicts it's now better to use class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
normhash: Optional[int] = Field(default=None, index=True, sa_type=BigInteger) |
Beta Was this translation helpful? Give feedback.
-
@yinziyan1206 This works if the field is not the primary key. If I want to create a primary key of type BigInteger, I get the exception:
e.g. with |
Beta Was this translation helpful? Give feedback.
-
@unidesigner uhhhh, you can make it a column in sqlalchemy just like this: |
Beta Was this translation helpful? Give feedback.
-
thanks @yinziyan1206 - this did the trick! |
Beta Was this translation helpful? Give feedback.
-
If you want to reference an customer_id: int = Field(None, foreign_key='customer.id', sa_column=Column(BigInteger())) |
Beta Was this translation helpful? Give feedback.
-
This doesn't create foreign key constrant because if sa_column is not undefined it is returned before any parameters are parsed. |
Beta Was this translation helpful? Give feedback.
-
To make it work you should do: |
Beta Was this translation helpful? Give feedback.
-
Just FYI, to get the from typing import Optional
from sqlalchemy import Column, BigInteger
from sqlmodel import Field, SQLModel
class Users(SQLModel, table=True):
id: Optional[int] = Field(default=None, sa_column=Column(BigInteger(), primary_key=True, autoincrement=True))
name: str This generates the following in postgres: CREATE TABLE public."users" (
id bigserial NOT NULL,
name varchar NOT NULL,
PRIMARY KEY (id)
); |
Beta Was this translation helpful? Give feedback.
-
I searched for a long time in the document, but did not find any relevant processing methods. I wonder if the document can introduce the data types of custom data databases for fields and the meaning of uncommon parameters in Field(). This is very helpful for beginners. |
Beta Was this translation helpful? Give feedback.
-
sqlmodel: I am getting the following error when trying this: from sqlalchemy import Column, BigInteger
from sqlmodel import Field, SQLModel
class Users(SQLModel, table=True):
id: int = Field(primary_key=True, sa_column=Column(BigInteger()))
so I switched to this and now things seem to work: from sqlalchemy import BigInteger
from sqlmodel import Field, SQLModel
class Users(SQLModel, table=True):
id: int = Field(primary_key=True, sa_type=BigInteger) |
Beta Was this translation helpful? Give feedback.
UPD YuriiMotov: to avoid conflicts it's now better to use
sa_type
: