kayak / pypika

PyPika is a python SQL query builder that exposes the full richness of the SQL language using a syntax that reflects the resulting query. PyPika excels at all sorts of SQL queries but is especially useful for data analysis.
http://pypika.readthedocs.io/en/latest/
Apache License 2.0
2.5k stars 295 forks source link

autoincrement int with create_table() #751

Open Krogsager opened 1 year ago

Krogsager commented 1 year ago

Hi, Given this SQL statement I can create a new table with an pk that increments itself.

CREATE TABLE job_queue (
    id INT IDENTITY(1,1) PRIMARY KEY,
    date_created DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    job_id VARCHAR(30)
);

Is there an equivalent way to do it with pypika?

query = Query \
    .create_table("job_queue") \
    .columns(
        Column("id", # question goes here
        Column("date_created", "DATETIME", nullable=False, default='CURRENT_TIMESTAMP'),
        Column("job_id", "VARCHAR(30)", nullable=True),
    ) \
    .unique("job_id") \
    .primary_key("id")

My bonus question: CURRENT_TIMESTAMP is database dependendt. It could also be NOW() or GETDATE(), but I'm using mssql/odbc. Does pypika have a variable for it?