Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLALCHEMY set default False nullable True

I have read few SQLAlchemy answers here, but I am still not clear about two scenarios.

What are exactly meant by these and what is the difference between these?

  • Setting default False and nullable = True for a column

    check = Column(Boolean, default=False, nullable=True)

  • Setting default True and nullable = False for a column

    check = Column(Boolean, default=True, nullable=False)

I am having confusing thoughts.

EDIT:

Here is the logic I am trying to implementing.

class Checking(Base):
    __tablename __ = 'Test'
    check=Column(Boolean, nullable=True)
    def __init__(self, check):
        self.check = check
    def test(self):
        if self.check:
            print("Success")
        else:
            print("Failed")

It is always working when check is becoming False i.e, printing 'Failed' but not working when check is becoming True. It is failing there.
The same is happening when the column is set to default=False.

If I set the column to default=True it is working when the column is becoming True but failing when column is becoming False--means vice-versa of the above.

PS: This is not the actual code but it is similar to the actual one that I am trying to achieve.

like image 687
trishnag Avatar asked Oct 16 '25 03:10

trishnag


1 Answers

default specifies what the default value for a column is when it is left empty, so default=True only makes sense for Boolean columns and will insert True if the column is empty.

nullable specifies if the value in the column can be NULL (or in python None) (which it will be if it is empty, without specifying the default).

like image 154
Jesse Bakker Avatar answered Oct 18 '25 17:10

Jesse Bakker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!