Reputation: 446
from xmlrpc.client import Boolean
from sqlalchemy import TIMESTAMP, Column, Integer, String
from sqlalchemy.sql.expression import text
from .database import Base
class Post(Base):
__tablename__ = 'posts'
id = Column(Integer, primary_key=True, nullable=False)
title = Column(String, unique=True, nullable=False)
content = Column(String, nullable=False)
published = Column(Boolean, server_default='True', nullable=False)
created_at= Column(TIMESTAMP(timezone=True), nullable= False, server_default=text)
I couldn't find the error. When I try to create the tables using models.Base.metadata.create_all(bind=engine)
it gives me the following error:
Traceback (most recent call last):
File "C:\Users\MINTU\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\sql\schema.py", line 125, in _init_items
spwd = item._set_parent_with_dispatch
AttributeError: type object 'bool' has no attribute '_set_parent_with_dispatch'
The above exception was the direct cause of the following exception:
File "C:\Users\MINTU\AppData\Local\Programs\Python\Python310\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
raise exception
sqlalchemy.exc.ArgumentError: 'SchemaItem' object, such as a 'Column' or a 'Constraint' expected, got <class 'bool'>
Upvotes: 0
Views: 218
Reputation: 2699
First, Boolean must not be imported from xmlrpc.client
but from sqlalchemy
Second, the name of your table is not written as tablename
but as __tablename__
.
Third, text
in TIMESTAMP
must be called with an argument that I would let you adapt to your needs
from sqlalchemy import TIMESTAMP, Column, Integer, String, Boolean
from sqlalchemy.sql.expression import text
from .database import Base
class Post(Base):
__tablename__ = 'posts'
id = Column(Integer, primary_key=True, nullable=False)
title = Column(String, unique=True, nullable=False)
content = Column(String, nullable=False)
published = Column(Boolean, server_default='True', nullable=False)
created_at= Column(TIMESTAMP(timezone=True), nullable= False, server_default=text('(now() at time zone "utc")'))
Upvotes: 1