newbie to sqlalchemy :not null constraint

565 views
Skip to first unread message

anusha kadambala

unread,
Feb 9, 2010, 10:57:24 PM2/9/10
to sqlal...@googlegroups.com
hello all,

I am newbie in sqlalchemy.I am thrilled by the sqlachemy features. But i got struck in the how to write the not null  for the following:

create table organisation(orgcode varchar(30) not null,orgname text not null,primary key(orgcode));

I have written the using declarative base as follows but dont know how to impose not null constraint

from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker,scoped_session

engine = create_engine('postgresql:///gkanusha', echo=False)
Base = declarative_base()
class Organisation(Base):
    __tablename__ = 'Organisation'
    orgcode = Column(String,primary_key=True)
    orgname = Column(String)
    def __init__(self,orgcode,orgname):
        self.orgcode = orgcode
        self.orgname = orgname

organisation_table = Organisation.__table__

metadata = Base.metadata
metadata.create_all(engine)
Session = scoped_session(sessionmaker(bind=engine))
Session.commit()

There are other columns also but i have taken two columns for simplicity.

Thanks in advance
--


Njoy the share of Freedom :)
Anusha Kadambala

Michael Trier

unread,
Feb 9, 2010, 11:23:30 PM2/9/10
to sqlal...@googlegroups.com

> hello all,
>
> I am newbie in sqlalchemy.I am thrilled by the sqlachemy features. But i got struck in the how to write the not null for the following:
>
> create table organisation(orgcode varchar(30) not null,orgname text not null,primary key(orgcode));
>
> I have written the using declarative base as follows but dont know how to impose not null constraint

You want to use the nullable=False argument.

http://www.sqlalchemy.org/docs/reference/sqlalchemy/schema.html?highlight=nullable

Thanks,

Michael

anusha kadambala

unread,
Feb 10, 2010, 4:35:25 AM2/10/10
to sqlal...@googlegroups.com
Thanks, Micheal for your help



--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

Reply all
Reply to author
Forward
0 new messages