Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » datetime, timestamp and check statements [RSS Feed]

#1 Nov. 4, 2005 20:45:54

[EMAIL P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

datetime, timestamp and check statements


Hi I am generating sql for sqlite3

class Language(meta.Model):

id = meta.CharField(maxlength=2, primary_key=True)
translation_key = meta.IntegerField()
status = meta.CharField(choices=LANGUAGE_STATUS_CHOICES, maxlength=8)
created = meta.DateTimeField(auto_now_add=True)
modified = meta.DateTimeField(auto_now=True)

class META:
db_table = "languages"

It generates the following:

CREATE TABLE languages (
id varchar(2) NOT NULL PRIMARY KEY,
translation_key integer NOT NULL,
status varchar(8) NOT NULL,
created time NOT NULL,
modified datetime NOT NULL

Why am I not getting a TIMESTAMP type instead of datetime. Preferably
I would like a timestamp with timezone.

Also choices are not generating a check statement like:
status VARCHAR(8) CHECK (status in ('active', 'inactive')),

How do I get this back into my SQL.

Many thanks
David

Offline

#2 Nov. 4, 2005 21:10:11

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

datetime, timestamp and check statements


On 11/4/05, <> wrote:
> Why am I not getting a TIMESTAMP type instead of datetime. Preferably
> I would like a timestamp with timezone.

As somebody who doesn't use SQLite, I must ask: What advantage does
timestamp offer over datetime? I can't find any mention of it onhttp://www.sqlite.org/datatype3.html.

> Also choices are not generating a check statement like:
> status VARCHAR(8) CHECK (status in ('active', 'inactive')),

The choices are implemented at the Python level, not at the database
level. Feel free to add the CHECK to your SQL manually.

django-admin.py sqlall myapp > myapp.sql
vim myapp.sql # add the CHECK
cat myapp.sql | sqlite # pipe the SQL into SQLite, however that's done

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

#3 Nov. 4, 2005 21:30:55

Waylan L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

datetime, timestamp and check statements


On 11/4/05, Adrian Holovaty <> wrote:
>
> On 11/4/05, <> wrote:
> > Why am I not getting a TIMESTAMP type instead of datetime. Preferably
> > I would like a timestamp with timezone.
>
> As somebody who doesn't use SQLite, I must ask: What advantage does
> timestamp offer over datetime? I can't find any mention of it on
>http://www.sqlite.org/datatype3.html.
>
> > Also choices are not generating a check statement like:
> > status VARCHAR(8) CHECK (status in ('active', 'inactive')),
>
> The choices are implemented at the Python level, not at the database
> level. Feel free to add the CHECK to your SQL manually.
>
> django-admin.py sqlall myapp > myapp.sql
> vim myapp.sql # add the CHECK
> cat myapp.sql | sqlite # pipe the SQL into SQLite, however that's done
>
> Adrian
>
> --
> Adrian Holovaty
> holovaty.com | djangoproject.com | chicagocrime.org
>

According to this page:http://www.sqlite.org/lang_createtable.htmlTIMESTAMP is not a type but a default value for a column as in
DEFAULT=TIMESTAMP.

And from this page:http://www.sqlite.org/different.htmlwe learn that
"SQLite... allows the user to store any value of any datatype into any
column regardless of the declared type of that column." Which basicly
means declaring a datatype does nothing for SQLite (except for an
INTEGER PRIMARY KEY or a few other rare exceptions).

I don't know, does django compensate for this? Making it work like
other DBs? Or should fairwinds be altering the tables for his needs?
Either way, it certainly makes the case for validating data before
writing it to your db.

--
----
Waylan Limberg

Offline

#4 Nov. 4, 2005 23:30:22

[EMAIL P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

datetime, timestamp and check statements


Hi there.

There is no advantage/disadvantage to the expression, this is mainly a
portability issue me since I generally use the same SQL for web based
applications with Postgres and with SQLite for embedded apps.

Waylan is right, SQLite has few types but and you can put pretty much
anything in. Its sort of an interesting database. TIMESTAMP is what I
anticipated based on the model. I have used SQLObject in the past to
generate SQL for SQLite with TIMESTAMP so perhaps it was out of this
experience that I was suprised it was not generated from Django.

How can time zone be included from my model?

Also. I was just reading about install and found something about
including .sql files in a sql folder in the project directory. I guess
the alter table statement could be included here. I am assuming the
sql from the install gets loaded after the tables are created.

On check statement is there any thought in including an EnumeratedField
type in models that could take the elements from the choice tuple and
create the check statement. I know MySQL uses a non stadard enumerated
type but the CHECK statement is standard SQL.

I have validation code I have written that I am planning on reusing
from my web apps. I was looking at seeing what could be done with
Django's validators once my models are all working the way I want. I am
using Django to write a cross platform Desktop app so am not certain
that I will be generating any views since I am writing GUI code. I
believe from what I have been reading that I would just have to import
the manipulators to implement validation (whether or not I am writing a
view). I am hoping to pass the validator messages to dialogs boxes in
my GUI this way.

Regards
David

Offline

#5 Nov. 5, 2005 00:50:28

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

datetime, timestamp and check statements


On 11/4/05, <> wrote:
> Waylan is right, SQLite has few types but and you can put pretty much
> anything in. Its sort of an interesting database. TIMESTAMP is what I
> anticipated based on the model. I have used SQLObject in the past to
> generate SQL for SQLite with TIMESTAMP so perhaps it was out of this
> experience that I was suprised it was not generated from Django.
>
> How can time zone be included from my model?

Hey David,

When Django outputs the CREATE TABLE statements, it looks at the
database engine you're currently using. Since you ran django-admin.py
with the sqllite database backend, it used sqlite syntax. If you
change your database engine to postgresql, the CREATE TABLE statements
will use postgresql-specific column types. Hope that clears things up!

> Also. I was just reading about install and found something about
> including .sql files in a sql folder in the project directory. I guess
> the alter table statement could be included here. I am assuming the
> sql from the install gets loaded after the tables are created.

Yes, the SQL from the install gets loaded after the tables are created.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

  • Root
  • » Django
  • » datetime, timestamp and check statements [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support