Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't sqlalchemy's default column value working

Tags:

I am using Postgresql 9.1 and SQLAlchemy 0.9.

The problem is, 'default=10' doesn't work.

My Code:

conn_str = 'postgresql://test:pass@localhost/test' engine = create_engine(conn_str) metadata = MetaData(bind=engine)  cols=[] cols += [Column('Name', VARCHAR(20), primary_key=True, nullable=False)] cols += [Column('age', INT, default=10, nullable=False )] Table('t1', metadata, *cols) metadata.create_all(engine) 

psql:

test=> \dS t1             Table "public.t1" Column |         Type          | Modifiers --------+-----------------------+----------- Name   | character varying(20) | not null age    | integer               | not null Indexes:     "t1_pkey" PRIMARY KEY, btree ("Name") 

I tried with an SQL statement directly and it should look like:

test=> \dS t2               Table "public.t2"  Column |         Type          | Modifiers --------+-----------------------+------------  Name   | character varying(10) | not null  age    | integer               | default 10 Indexes:     "t2_pkey" PRIMARY KEY, btree ("Name") 

What am I doing wrong?

like image 583
Aylwyn Lake Avatar asked Dec 03 '13 10:12

Aylwyn Lake


People also ask

What is Server default in SQLAlchemy?

A client-side SQL expression, a server_default value, and server-side implicit defaults and triggers all have the server generate the default, which then must be fetched by the client if you want to be able to access it in the same SQLAlchemy session.

What does index mean in SQLAlchemy?

SQLAlchemy Index is used for assigning the identifiers for each of the particular row getting stored inside a table. We can have indexing based on the single column or collection of two or more columns together acting as an index to the table rows.

What is DB relationship SQLAlchemy?

The relationship function is a part of Relationship API of SQLAlchemy ORM package. It provides a relationship between two mapped classes. This corresponds to a parent-child or associative table relationship.


1 Answers

instead of using default, use server_default... fixed the problem for me https://docs.sqlalchemy.org/en/latest/core/defaults.html?highlight=schema%20column#sqlalchemy.schema.ColumnDefault

like image 125
tbischel Avatar answered Sep 20 '22 05:09

tbischel