Brownerg
Brownerg

Reputation: 21

Django Char vs. Varchar

Please Help, I have an existing database that uses a char(12) as its PK.

CREATE TABLE sop_customer ( customer_id char(12) NOT NULL, PRIMARY KEY (customer_id), ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I then create a Django Model and create a model based on inspectdb (awesome tool :-))

The problem is when I create another table with a foreign key to this table, the new table will use varchar() and MySQL at the database level will not honour the relationship because of the different types (char vs .varchar) HELP!

I am in the process of trying: 1 - Update the existing db to Varchar (seems like a rather extreme operation just to get this thing to work :-0) 2 - Get Django's foreign Key class to recognise the type properly (I understand ORM agnostisism etc). 2 - Force Django to create a char field and then add the constraint after in Django, although I cant see how this could be achieved without heavy lifting 3 - Not use Django

Help!

Upvotes: 0

Views: 753

Answers (1)

Brownerg
Brownerg

Reputation: 21

Muppet! - The original table is UTF8. The second table was being created as latin1 CHARACTER SET :-0.

CHAR - VARCHAR can create FK relationships both at the DB level and Django level :-)

Upvotes: 1

Related Questions