Table doesn't creates with default value [SQLalchemy] - postgresql

I have table
users = Table(
"users",
metadata,
Column("id", Integer, primary_key=True),
Column("username", String(32), unique=True),
Column("password", String(64)),
Column("games_all", Integer, default=0),
Column("games_won", Integer, default=0),
Column("created_at", DateTime, default=datetime.utcnow)
)
and when i do
metadata.create_all(bind=engine)
the table is created but with an empty default field.
Image
I want the table to be created with the default values that I specified.

The column default argument is the default value set on the Python side when creating a users instance. If you want the default value to be set on the database side, use server_default.

Related

CREATE FUNCTION failed because a column name is not specified for column 1. error for the Multiple parameter of function

Wanted to create the multiple parameter of function but it gives me this error:
CREATE FUNCTION failed because a column name is not specified for
column 1.
Code below:
create function dmt.Impacted(
#nameOfColumn varchar , #nameOfParam varchar)
returns table
as
return
(select
case when '['+#nameOfColumn+']' is null or len(rtrim('['+#nameOfColumn+']')) = 0
then Convert(nvarchar(2),0)
else
#nameOfParam end from employee) ;
As the error message clearly said, the column in the returned result need a name. Either give it an alias in the SELECT like
SELECT CASE
...
END a_column_name
...
or define it in the declaration of the return type as in
...
RETURNS TABLE
(a_column_name nvarchar(max)
...
As you can see in the second form you have to specify a data type. As your current code doesn't make much sense now I cannot figure out what is the right one there. You'd need to amend it.
Note, that len(rtrim('['+#nameOfColumn+']')) = 0 is never true as len(rtrim('['+#nameOfColumn+']')) is either NULL, when #nameOfColumn is NULL or at least 2 because of the added brackets.
If #nameOfColumn is supposed to be a column name you shouldn't use varchar (especially without a length specified for it) but sysname which is a special type for object names.
Either way you should define a length for #nameOfColumn and #nameOfParam as just varchar without any length means varchar(1), which is probably not what you want. And maybe instead of varchar you want nvarchar.
You may also want to look into quotename().
Define name of column in SELECT statement :
(select case when '['+#nameOfColumn+']' is null or
len(rtrim('['+#nameOfColumn+']')) = 0
then Convert(nvarchar(2),0)
else #nameOfParam
end as name_column -- define column name
from employee)
Also, your function parameter has no data length, by default it will accept only 1 character #nameOfColumn varchar , #nameOfParam varchar & rest will trim.

cakePHP find() returns field name instead of just the column values

I'm trying to retrieve data from my database using the find() method.
But when I use find('all') it returns the column value and column name together, like this: { "date_1": "2015-25-12"}, of course, I want only the values from the column.
If I use find('list'), it comes back empty.
This is how I'm trying to retrieve my data:
$date1 = $this->Stocks->find('all', ['fields' => ['Stocks.date_1'], 'conditions' => ['Stocks.families_id' => $id]]);
This is my table:
CREATE TABLE stocks
(
families_id integer,
date_1 date NOT NULL,
date_2 date,
created timestamp without time zone,
modified timestamp without time zone,
id serial NOT NULL,
CONSTRAINT stocks_pkey PRIMARY KEY (id)
)
Everything else is working fine, but this one part.
I'm using PostgreSQL.
Although you problem was related to displayField.
To select a single column or a set of columns you can do like this
$date1 = $this->Stocks->find()
->select(['date_1'])
->where(['families_id' => $id]);

Return custom type x composed of custom type y in atomic table format PostgreSQL

I have a custom type:
CREATE TYPE single_journey AS (
tram_id integer,
departure_station text,
departure_time time,
destination_station text,
arrival_time time);
and another custom type:
CREATE TYPE double_journey AS (
journey_one single_journey,
journey_two single_journey);
The second custom type exists because I have a function that can return two single_journey types together.
The problem is, the output is like this:
journey_one journey_two
single_journey single_journey
---------------------------------------- ----------------------------------------
(39,StationX,11:00:00,StationY,12:00:00) (40,StationY,12:30:00,StationZ,13:00:00
What I desire is each column of each single_journey in double_journey to be in it's own separate column. Is this possible or would I have to re-create the double_journey type with the columns I want e.g. departure_station_1, departure_time_1...arrival_station_2, arrival_time_2 etc...?
Instead of
SELECT journey_one, journey_two
USE
SELECT journey_one,
(journey_two).field1,
(journey_two).field2,
(journey_two).field3, etc

Is it possible to set UserDefinedTableType column default value?

I have created my own TableType, is it possible to set a date column default in order to not call GetDate() within every insert?
It's quite straightforward, as in case of regular tables, but you can not assign a name to that constraint:
CREATE TYPE TestType AS TABLE
( ID INT,
CreatedDate DATETIME DEFAULT(GETDATE())
)

Is it possible to create a DEFAULT clause with an incrementing identifier?

I would like to know whether it is possible to achieve something like the following in PostgreSQL:
I have a table named Dossier (means "folder" in English), with the the following table structure: name, taille,...
Now, what I'd like to have on my table is a condition such that each time a new instance of Dossier is created, the value in the name column is automatically supplemented with a self-incrementing identifier like so: ref00001, ref00002 etc. (To clarify, after the second insertion, the value for the name column should be ref00002 automatically...)
CREATE SEQUENCE seq_ref;
CREATE TABLE dossier
(
ref TEXT NOT NULL PRIMARY KEY DEFAULT 'ref' || NEXTVAL('seq_ref'),
value TEXT
);
If you want zero-padded numbers, use this:
CREATE SEQUENCE seq_ref;
CREATE TABLE dossier
(
ref TEXT NOT NULL PRIMARY KEY DEFAULT 'ref' || LPAD(NEXTVAL('seq_ref')::TEXT, 10, '0'),
value TEXT
);